如何在图库拖动时更改列表视图数据集?
How to change listview DataSet on Gallery drag?
我有一个画廊,其中包含代表类别的自定义视图,根据所选画廊的类别,列表视图应显示一个或另一个数据集,它正在工作,但显示的数据集与所选类别不对应,
这是图库的适配器:
public class GalleryViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<TreeNode> mNodes;
private HomeFragment mFrag;
private ListView mList;
public GalleryViewAdapter(Context c, ArrayList<TreeNode> mNodes, HomeFragment mFrag, ListView galleryList) {
mContext = c;
this.mNodes = mNodes;
this.mFrag = mFrag;
mList = galleryList;
}
@Override
public int getCount() {
return mNodes.size();
}
@Override
public Object getItem(int position) {
return mNodes.get(position);
}
@Override
public long getItemId(int position) {
TreeNode childNode = (TreeNode) mNodes.get(position);
if (childNode.getReference() instanceof Categoria) {
return Long.valueOf(((Categoria) mNodes.get(position).getReference()).idCat);
} else {
return Long.valueOf(((Contenido) mNodes.get(position).getReference()).idCon);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TreeNode childNode = (TreeNode) mNodes.get(position);
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = wm.getDefaultDisplay().getWidth();
if (childNode.getReference() instanceof Categoria) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fragment_gallery_item, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.fragment_gallery_item_imageview);
TextView mTextView = (TextView) convertView.findViewById(R.id.fragment_gallery_item_textView);
Categoria mCat = (Categoria) childNode.getReference();
imageView.setImageBitmap(Tools.scaleImageInPixels(Tools.createBitmapFromBase64(mCat.icon64.getBytes()),
screenWidth));
mTextView.setText(mCat.nombreCat);
int newHeight = mFrag.getActivity().getWindowManager().getDefaultDisplay().getHeight() / 4;
int orgWidth = imageView.getDrawable().getIntrinsicWidth();
int orgHeight = imageView.getDrawable().getIntrinsicHeight();
int newWidth = ((int) Math.floor((orgWidth * newHeight) / orgHeight) / 2);
imageView.setMaxHeight(newHeight);
imageView.setMaxWidth(newWidth);
GalleryListAdapter mListAdapter = new GalleryListAdapter(mFrag.getActivity(), childNode, mFrag);
mList.setAdapter(mListAdapter);
} else {
}
return convertView;
}
}
这是我的 galleryListViewAdapter
public class GalleryListAdapter extends BaseAdapter {
private Context mContext;
private TreeNode mNode;
private HomeFragment mFrag;
public GalleryListAdapter(Context mContext, TreeNode mNode, HomeFragment mFrag) {
this.mContext = mContext;
this.mNode = mNode;
this.mFrag = mFrag;
}
@Override
public int getCount() {
return mNode.getChildren().size();
}
@Override
public Object getItem(int position) {
return mNode.getChildren().get(position);
}
@Override
public long getItemId(int position) {
if (mNode.getReference() instanceof Categoria) {
return Long.valueOf(((Categoria) mNode.getReference()).idCat);
} else {
return Long.valueOf(((Contenido) mNode.getReference()).idCon);
}
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (mNode.getReference() instanceof Categoria) {
mNode = (TreeNode) mNode.getChildren().get(position);
}
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.fragment_list_carrousel_item, parent, false);
}
TextView autorView = (TextView) rowView.findViewById(R.id.firstLine);
TextView textView = (TextView) rowView.findViewById(R.id.secondLine);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
Contenido mCon = (Contenido) mNode.getReference();
autorView.setText(mCon.tituloCon);
textView.setText("");
imageView.setImageBitmap(Tools.scaleImage(Tools.getParentIconRecursive(mNode),
(int) MainActivity.getInstance().getAttributeDimension(R.attr.listPreferredItemHeight)));
int newHeight = mFrag.getActivity().getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = imageView.getDrawable().getIntrinsicWidth();
int orgHeight = imageView.getDrawable().getIntrinsicHeight();
int newWidth = ((int) Math.floor((orgWidth * newHeight) / orgHeight) / 2);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
newWidth, newHeight);
imageView.setLayoutParams(params);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFrag.goToNode(mNode);
}
});
return rowView;
}
}
非常感谢!
您必须像这样在 Gallery
上设置 OnItemSelectedListener
:
private Gallery.OnItemSelectedListener _scrollListener = new Gallery.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
//execute the method that refresh the listview
fillListView(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
};
我有一个画廊,其中包含代表类别的自定义视图,根据所选画廊的类别,列表视图应显示一个或另一个数据集,它正在工作,但显示的数据集与所选类别不对应,
这是图库的适配器:
public class GalleryViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<TreeNode> mNodes;
private HomeFragment mFrag;
private ListView mList;
public GalleryViewAdapter(Context c, ArrayList<TreeNode> mNodes, HomeFragment mFrag, ListView galleryList) {
mContext = c;
this.mNodes = mNodes;
this.mFrag = mFrag;
mList = galleryList;
}
@Override
public int getCount() {
return mNodes.size();
}
@Override
public Object getItem(int position) {
return mNodes.get(position);
}
@Override
public long getItemId(int position) {
TreeNode childNode = (TreeNode) mNodes.get(position);
if (childNode.getReference() instanceof Categoria) {
return Long.valueOf(((Categoria) mNodes.get(position).getReference()).idCat);
} else {
return Long.valueOf(((Contenido) mNodes.get(position).getReference()).idCon);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TreeNode childNode = (TreeNode) mNodes.get(position);
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = wm.getDefaultDisplay().getWidth();
if (childNode.getReference() instanceof Categoria) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fragment_gallery_item, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.fragment_gallery_item_imageview);
TextView mTextView = (TextView) convertView.findViewById(R.id.fragment_gallery_item_textView);
Categoria mCat = (Categoria) childNode.getReference();
imageView.setImageBitmap(Tools.scaleImageInPixels(Tools.createBitmapFromBase64(mCat.icon64.getBytes()),
screenWidth));
mTextView.setText(mCat.nombreCat);
int newHeight = mFrag.getActivity().getWindowManager().getDefaultDisplay().getHeight() / 4;
int orgWidth = imageView.getDrawable().getIntrinsicWidth();
int orgHeight = imageView.getDrawable().getIntrinsicHeight();
int newWidth = ((int) Math.floor((orgWidth * newHeight) / orgHeight) / 2);
imageView.setMaxHeight(newHeight);
imageView.setMaxWidth(newWidth);
GalleryListAdapter mListAdapter = new GalleryListAdapter(mFrag.getActivity(), childNode, mFrag);
mList.setAdapter(mListAdapter);
} else {
}
return convertView;
}
}
这是我的 galleryListViewAdapter
public class GalleryListAdapter extends BaseAdapter {
private Context mContext;
private TreeNode mNode;
private HomeFragment mFrag;
public GalleryListAdapter(Context mContext, TreeNode mNode, HomeFragment mFrag) {
this.mContext = mContext;
this.mNode = mNode;
this.mFrag = mFrag;
}
@Override
public int getCount() {
return mNode.getChildren().size();
}
@Override
public Object getItem(int position) {
return mNode.getChildren().get(position);
}
@Override
public long getItemId(int position) {
if (mNode.getReference() instanceof Categoria) {
return Long.valueOf(((Categoria) mNode.getReference()).idCat);
} else {
return Long.valueOf(((Contenido) mNode.getReference()).idCon);
}
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (mNode.getReference() instanceof Categoria) {
mNode = (TreeNode) mNode.getChildren().get(position);
}
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.fragment_list_carrousel_item, parent, false);
}
TextView autorView = (TextView) rowView.findViewById(R.id.firstLine);
TextView textView = (TextView) rowView.findViewById(R.id.secondLine);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
Contenido mCon = (Contenido) mNode.getReference();
autorView.setText(mCon.tituloCon);
textView.setText("");
imageView.setImageBitmap(Tools.scaleImage(Tools.getParentIconRecursive(mNode),
(int) MainActivity.getInstance().getAttributeDimension(R.attr.listPreferredItemHeight)));
int newHeight = mFrag.getActivity().getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = imageView.getDrawable().getIntrinsicWidth();
int orgHeight = imageView.getDrawable().getIntrinsicHeight();
int newWidth = ((int) Math.floor((orgWidth * newHeight) / orgHeight) / 2);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
newWidth, newHeight);
imageView.setLayoutParams(params);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFrag.goToNode(mNode);
}
});
return rowView;
}
}
非常感谢!
您必须像这样在 Gallery
上设置 OnItemSelectedListener
:
private Gallery.OnItemSelectedListener _scrollListener = new Gallery.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
//execute the method that refresh the listview
fillListView(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
};