我解决了这个问题。您可以使用Handler向Fragment发送消息,然后在片段中使用adapter.notifyDataSetChanged()。
我想在点击它时更改ImageView的图片。这些组件都在一个适配器中。我还使用了adapter.notifyDataSetChanged();该适配器在片段中使用并将其放入GridView.I found他们说的一些文章使用adapter.notifyDataSetChanged()可以告诉程序更新其UI。但对我来说它不起作用。我该怎么办?先感谢您。 这是我的代码和xml:
public class ResItemAdapter extends BaseAdapter{
private ViewHolder holder;
private LayoutInflater inflater;
private Context context;
private List<ResBean> lists;
private ResBean bean;
private int appType;
private DBUtil db;
private ResItemAdapter adapter;
public ResItemAdapter(Context con)
{
this.context=con;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.adapter=this;
}
public ResItemAdapter(Context con,List<ResBean> lists)
{
this.lists = lists;
this.context=con;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.adapter=this;
}
public ResItemAdapter(Context con,List<ResBean> lists,int appType,DBUtil db)
{
this.context=con;
this.lists=lists;
this.db=db;
this.appType=appType;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.adapter=this;
}
@Override
public int getCount()
{
return lists.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if(convertView==null)
{
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.res_item,null);
convertView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,150));
holder.resLayout = (RelativeLayout)convertView.findViewById(R.id.res_item_select_s);
holder.resItemImage=(ImageView)convertView.findViewById(R.id.res_item_image_s);
holder.resItemName=(TextView)convertView.findViewById(R.id.res_item_name_s);
holder.resItemDingyue=(TextView)convertView.findViewById(R.id.res_item_dingyue_s);
holder.resItemStar=(ImageView)convertView.findViewById(R.id.res_item_star_s);
holder.resItemSelectedBtn=(ImageView)convertView.findViewById(R.id.res_item_selected_btn_s);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
bean=lists.get(position);
String imagePath = FileUtils.getItemImagePath(lists.get(position).getPath());
if(imagePath.equals("")){
holder.resItemImage.setBackgroundResource(R.drawable.moren);
}else{
try {
holder.resItemImage.setImageBitmap(BitmapFactory.decodeFile(FileUtils.getItemImagePath(lists.get(position).getPath())));
} catch (Exception e) {
e.printStackTrace();
}
}
holder.resItemName.setText(bean.getName());
holder.resItemDingyue.setText(bean.getBookCount()+"人订阅");
setStar(bean);
if(bean.isBook())
{
holder.resItemSelectedBtn.setImageDrawable(context.getResources().getDrawable(R.drawable.but_icon_subscribed));
}else{
holder.resItemSelectedBtn.setImageDrawable(context.getResources().getDrawable(R.drawable.content_add_btn_empty));
}
holder.resLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
PopWindow mPop = new PopWindow(context,lists.get(position));
mPop.setResDetailPop(holder.resLayout);
}
});
holder.resItemSelectedBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
bean=lists.get(position);
if(db.insertBookOrder(bean,appType))
{
Log.d("test","resId="+bean.getId()+" position="+position);
holder.resItemSelectedBtn.setImageDrawable(context.getResources().getDrawable(R.drawable.but_icon_subscribed));
adapter.notifyDataSetChanged();
}
}
});
return convertView;
}
private void setStar(ResBean bean)
{
int stars=bean.getRating();
switch(stars)
{
case 0:
holder.resItemStar.setBackgroundResource(R.drawable.small_star0);
break;
case 1:
holder.resItemStar.setBackgroundResource(R.drawable.small_star1);
break;
case 2:
holder.resItemStar.setBackgroundResource(R.drawable.small_star2);
break;
case 3:
holder.resItemStar.setBackgroundResource(R.drawable.small_star3);
break;
case 4:
holder.resItemStar.setBackgroundResource(R.drawable.small_star4);
break;
case 5:
holder.resItemStar.setBackgroundResource(R.drawable.small_star5);
break;
}
}
class BitmapTask extends AsyncTask<Integer,Void,Bitmap>
{
private final WeakReference<ImageView> reference;
private String path;
public BitmapTask(ImageView imageView,String path)
{
reference=new WeakReference<ImageView>(imageView);
this.path=path;
}
@Override
protected Bitmap doInBackground(Integer... arg0)
{
try {
return BitmapFactory.decodeStream(new FileInputStream(FileUtils.getImagePath(path)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
if(reference!=null&& bitmap!=null)
{
final ImageView view=reference.get();
if(view!=null)
{
view.setImageBitmap(bitmap);
}
}
}
}
class ViewHolder
{
public RelativeLayout resLayout;
public ImageView resItemImage;
public ImageView resItemStar;
public TextView resItemName;
public TextView resItemDingyue;
public ImageView resItemSelectedBtn;
}}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/res_item_select_s"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@color/background_default" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dip"
android:layout_marginTop="12dip" >
<ImageView
android:id="@+id/res_item_image_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:contentDescription="@string/description"/>
<RelativeLayout
android:id="@+id/res_item_content_s"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/res_item_image_s"
android:layout_marginLeft="8dip"
android:layout_marginTop="5dip"
android:layout_toRightOf="@id/res_item_image_s"
android:orientation="vertical" >
<TextView
android:id="@+id/res_item_name_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="20sp" />
<ImageView
android:id="@+id/res_item_star_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/res_item_name_s"
android:layout_centerVertical="true"
android:contentDescription="@string/description" />
<TextView
android:id="@+id/res_item_dingyue_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dip"
android:textSize="12sp" />
</RelativeLayout>
<ImageView
android:id="@+id/res_item_selected_btn_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#00000000"
android:contentDescription="@string/description"/>
</RelativeLayout>
答案 0 :(得分:0)
为什么不尝试选择。我认为你可以通过将abc.xml放在drawable文件夹中来实现这一点,你可以在点击项目之前和之后给出图像。然后在程序中使用R.drawable.abc
abc.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/selectedimage" android:state_pressed="true"
android:state_selected="true" />
<item android:drawable="@drawable/unselected" />
</selector>
答案 1 :(得分:0)
我解决了这个问题。您可以使用Handler向Fragment发送消息,然后在片段中使用adapter.notifyDataSetChanged()。
任何人都可以告诉我,为什么我应该在我的片段中放置adapter.notifyDataSetChanged()?我在活动中使用的适配器中使用的最后一个APP是正常的。