我在代码中使用了Fragment
。我必须在片段中显示自定义ListView
。但我想处理此Button
中onClickListener
的{{1}}事件。
如何在ListView
类中使用这些Button
组件?由于Fragment
的行布局是另一种布局,我可以在ListView
类中使用它。
这是我的代码:
我的脆弱班级
Fragment
适配器类
public class VoteFragment extends Fragment
{
ListView list_model;
View rootView;
private String models_name[]={"MehararPaurKamali", "Elmira Panajavi","Hanay YavariMoghadam","MehararPaurKamali"};
private String models_adress[]={"Icon Girmfriend", "Deccan","ShivajiNagar","India"};
int model_images[]={R.drawable.karina,R.drawable.karina,R.drawable.karina,R.drawable.karina};
ArrayList<ModelListItem> modelList;
ModelListItem modelItem;
Button btn_home_model_plus;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
modelList=new ArrayList<ModelListItem>();
for(int i=0;i<models_name.length;i++)
{ modelItem=new ModelListItem();
modelItem.setModelname(models_name[i]);
modelItem.setModelAdress(models_adress[i]);
modelItem.setModel_image(model_images[i]);
modelList.add(modelItem);
}
rootView = inflater.inflate(R.layout.frag_vote,container, false);
setUI();
//Public void myClickHandler(){}
return rootView;
}
private void setUI()
{
list_model=(ListView)rootView.findViewById(R.id.list_home_model);
btn_home_model_plus=(Button)getView().findViewById(R.id.btn_home_model_plus);
}
}
列表视图行布局
public class HomeModelListAdapter extends BaseAdapter implements OnClickListener
{
private ArrayList<ModelListItem> modelList;
private Context context;
Button btn_home_model_plus,btn_home_model_vote;
FragmentManager fragmentmanager;
public HomeModelListAdapter(Context context, ArrayList<ModelListItem> modelList)
{
this.context=context;
this.modelList=modelList;
}
@Override
public int getCount()
{
return modelList.size();
}
@Override
public Object getItem(int arg0)
{
return modelList.get(arg0);
}
@Override
public long getItemId(int arg0)
{
return arg0;
}
/*private view holder class*/
private class ViewHolder
{
ImageView imgModelimage;
TextView txtModeName;
TextView txtModelAdress;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2)
{ ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.fram_home_list, null);
holder = new ViewHolder();
holder.txtModeName= (TextView) convertView.findViewById(R.id.txt_home_model_name);
holder.txtModelAdress = (TextView) convertView.findViewById(R.id.txt_home_model_adress);
holder.imgModelimage = (ImageView) convertView.findViewById(R.id.img_home_model);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.txtModeName.setText(modelList.get(position).getModelname());
holder.txtModelAdress.setText(modelList.get(position).getModelAdress());
holder.imgModelimage.setImageResource(modelList.get(position).getModel_image());
btn_home_model_plus=(Button)convertView.findViewById(R.id.btn_home_model_plus);
btn_home_model_vote=(Button)convertView.findViewById(R.id.btn_home_model_vote);
btn_home_model_plus.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View v)
{ Fragment fragment = null;
switch (v.getId())
{
case R.id.btn_home_model_plus:
System.out.println("Hi.................");
fragment = new ModelProfileDetailFragment();
break;
case R.id.btn_home_model_vote:
break;
default:
break;
}
if (fragment != null)
{
fragmentmanager.beginTransaction().add(R.id.fram_home, fragment).commit();
//notifyDataSetChanged();
}
}
请给我一些建议。
答案 0 :(得分:1)
好吧我觉得你的问题很简单。您需要做的就是像这样编辑HomeModelListAdapter
的{{1}}方法类。
getView()
希望这可以解决您的问题。
答案 1 :(得分:0)
尝试此操作以访问您的片段管理器 ((Activity)context).getFragmentManager();
我想知道您的列表视图是如何与您的片段相关联的。