gpt4 book ai didi

android - 如何处理Fragment类ListView的按钮事件?

转载 作者:行者123 更新时间:2023-11-30 03:07:41 25 4
gpt4 key购买 nike

我在代码中使用了 Fragment。我必须在 fragment 中显示自定义 ListView。但我想处理 ButtononClickListener 事件,它位于 ListView 中。
如何使用这些 Button Fragment 类中的组件?因为 ListView 的行布局是另一种布局,所以我可以在 Fragment 类中使用它。
这是我的代码:

我的 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();
}
}

ListView 行布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="#ffffff"
android:padding="@dimen/s5dp" >

<RelativeLayout
android:id="@+id/rel_image_layout"
android:layout_width="100dp"
android:layout_height="100dp" >

<ImageView
android:id="@+id/img_home_model"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/karina" />
</RelativeLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/s10dp"
android:layout_toLeftOf="@+id/rel_right_layout"
android:layout_toRightOf="@+id/rel_image_layout"
android:orientation="vertical"
android:weightSum="100" >

<TextView
android:id="@+id/txt_home_model_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="35"
android:fontFamily="Roboto"
android:gravity="top"
android:lines="1"
android:text="Mehrara Pourkamli"
android:textColor="#606366"
android:textSize="@dimen/s17sp" />

<TextView
android:id="@+id/txt_home_model_adress"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="left"
android:layout_weight="35"
android:drawableLeft="@drawable/pointer"
android:fontFamily="Roboto"
android:lines="1"
android:text=" Richmond Hill, ON"
android:textColor="#898e91"
android:textSize="@dimen/s12sp" />

<RelativeLayout
android:id="@+id/rel_right_layout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_gravity="bottom"
android:layout_weight="30" >

<Button
android:id="@+id/btn_home_model"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="#ca1b76"
android:gravity="bottom"
android:padding="@dimen/s5dp"
android:text="Vote Now"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>

<RelativeLayout
android:id="@+id/rel_right_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true" >

<Button
android:id="@+id/btn_home_model_plus"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:background="@drawable/btn_plus"
android:onClick="myClickHandler"
android:padding="@dimen/s5dp" />
</RelativeLayout>

</RelativeLayout>

请给我建议。

最佳答案

好的,我认为你的问题很简单。您需要做的就是像这样编辑您的 HomeModelListAdaptergetView() 方法类。

public class HomeModelListAdapter extends BaseAdapter {
.....
.....
@Override
public View getView(int position, View convertView, ViewGroup arg2)
{
.....
btn_home_model_plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff here
}
});
btn_home_model_vote.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff here
}
});
....
}
....
}

希望这能解决您的问题。

关于android - 如何处理Fragment类ListView的按钮事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21520531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com