本文介绍了Onclicklistner不工作的片段列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在listfragment自定义适配器,还可以设置onclicklistner的列表视图列表视图。但Onclicklistner不起作用。请人帮助我。听到我的code,

 公共类BasicFragment扩展ListFragment {


ListView的LV;
MyCustomAdapter适配器;


@覆盖
公共无效的onCreate(包SI){
    super.onCreate(SI);
}

@覆盖
公共无效onActivityCreated(包B){
    super.onActivityCreated(B);

}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

    查看查看= inflater.inflate(R.layout.fragment_basic,集装箱,假);
    LV =(ListView控件)view.findViewById(android.R.id.list);
    FetchedData DT = FetchedData.StaticDataTransfer();
    RecepiesProperties [] AryObjaz = DT.getData();
    的getData(AryObjaz);
    适配器=新MyCustomAdapter(getActivity(),R.layout.listview_layout,
            数据集);
    lv.setAdapter(适配器);

    lv.setOnItemClickListener(新OnItemClickListener(){

        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                长ARG3){
            // TODO自动生成方法存根
            吐司T = Toast.makeText(getActivity(),消息,
                    Toast.LENGTH_SHORT);
            t.show();

        }
    });

    返回查看;
}}
 

MyCustomAdapter.java

 公共类MyCustomAdapter扩展ArrayAdapter<配方> {

上下文语境;
INT layoutResourceId;
配方数据[] = NULL;
字体字样;
公共ImageLoader的ImageLoader的;

公共MyCustomAdapter(上下文的背景下,INT textViewResourceId,
        食谱[]数据集){
    超(背景下,textViewResourceId,数据集);
    this.layoutResourceId = textViewResourceId;
    this.context =背景;
    this.data =数据集;
    ImageLoader的=新ImageLoader的(context.getApplicationContext());
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    查看排= convertView;

    LayoutInflater充气=((活动)上下文).getLayoutInflater();
    行= inflater.inflate(layoutResourceId,父母,假);
    RecipesHolder持有人=新RecipesHolder();
    holder.imgIcon =(ImageView的)row.findViewById(R.id.imageView1);
    holder.txtTitle =(TextView中)row.findViewById(R.id.title);
    holder.category =(TextView中)row.findViewById(R.id.category);
    holder.source =(TextView中)row.findViewById(R.id.source);
    holder.country =(TextView中)row.findViewById(R.id.country);
    holder.readytime =(TextView中)row.findViewById(R.id.readytime);
    holder.tips =(按钮)row.findViewById(R.id.tips);
    holder.fav =(按钮)row.findViewById(R.id.fav);

    食谱AP =数据[位置]

    imageLoader.DisplayImage(ap.getIMAGENAME240(),holder.imgIcon);
    holder.txtTitle.setText(ap.getNAME());
    holder.category.setText(ap.getCATEGORY());
    holder.source.setText(ap.getSOURCE());
    holder.country.setText(ap.getCOUNTRY());
    holder.readytime.setText(ap.getREADYTIME());


    返回行;
}

静态类RecipesHolder {
    ImageView的imgIcon;
    TextView的txtTitle;
    TextView的类;
    TextView的来源;
    TextView的国家;
    TextView的readytime;
    按钮的提示;
    巴顿最爱;
}}
 

// listview_layout.xml

 < RelativeLayout的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>< ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_width =100dp
    机器人:layout_height =100dp
    机器人:layout_alignParentLeft =真
    机器人:可聚焦=真
    机器人:layout_alignParentTop =真
    机器人:layout_marginLeft =5DP
    机器人:layout_marginTop =10dp/>

<的TextView
    机器人:ID =@ + ID / readytime
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBaseline =@ + ID /国
    机器人:layout_alignBottom =@ + ID /国
    机器人:layout_marginLeft =73dp
    机器人:layout_toRightOf =@ + ID /国
    机器人:可聚焦=真
    机器人:文本=TextView的
    机器人:文字颜色=#000/>

<的TextView
    机器人:ID =@ + ID /国
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBottom =@ + ID / imageView1
    机器人:layout_alignLeft =@ + ID /源
    机器人:可聚焦=真
    机器人:文本=TextView的
    机器人:文字颜色=#000/>

<的TextView
    机器人:ID =@ + ID /源
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID /国
    机器人:layout_alignLeft =@ + ID /类别
    机器人:可聚焦=真
    机器人:文本=TextView的
    机器人:文字颜色=#000/>

<的TextView
    机器人:ID =@ + ID /类别
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID /源
    机器人:layout_alignLeft =@ + ID /标题
    机器人:文本=TextView的
    机器人:可聚焦=真
    机器人:文字颜色=#000/>

<的TextView
    机器人:ID =@ + ID /标题
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID /类别
    机器人:layout_toRightOf =@ + ID / imageView1
    机器人:文本=TextView的
    机器人:可聚焦=真
    机器人:文字颜色=#000/>

<按钮
    机器人:ID =@ + ID /最爱
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID /提示
    机器人:layout_below =@ + ID /源
    机器人:可聚焦=真
    机器人:背景=@可绘制/最爱/>

<按钮
    机器人:ID =@ + ID /提示
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentRight =真
    机器人:layout_alignTop =@ + ID / textView1
    机器人:layout_marginRight =14dp
    机器人:背景=@可绘制/黄/>< / RelativeLayout的>
 

解决方案

终于解决了这个问题,当列表视图中的所有控件(按钮,textviews)设置为可成为焦点的假。

i have a listview with custom adapter in listfragment and also set onclicklistner for listview. but Onclicklistner does not work. please anybody help me.Hear my code,

public class BasicFragment extends ListFragment {


ListView lv;
MyCustomAdapter adapter;


@Override
public void onCreate(Bundle si) {
    super.onCreate(si);
}

@Override
public void onActivityCreated(Bundle b) {
    super.onActivityCreated(b);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_basic, container, false);
    lv = (ListView) view.findViewById(android.R.id.list);
    FetchedData DT = FetchedData.StaticDataTransfer();
    RecepiesProperties[] AryObjaz = DT.getData();
    getdata(AryObjaz);
    adapter = new MyCustomAdapter(getActivity(), R.layout.listview_layout,
            Dataset);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast t = Toast.makeText(getActivity(), "Message",
                    Toast.LENGTH_SHORT);
            t.show();

        }
    });

    return view;
}}

MyCustomAdapter.java

public class MyCustomAdapter extends ArrayAdapter<Recipes> {

Context context;
int layoutResourceId;
Recipes data[] = null;
Typeface typeface;
public ImageLoader imageLoader;

public MyCustomAdapter(Context context, int textViewResourceId,
        Recipes[] dataset) {
    super(context, textViewResourceId, dataset);
    this.layoutResourceId = textViewResourceId;
    this.context = context;
    this.data = dataset;
    imageLoader = new ImageLoader(context.getApplicationContext());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);
    RecipesHolder holder = new RecipesHolder();
    holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
    holder.txtTitle = (TextView) row.findViewById(R.id.title);
    holder.category = (TextView) row.findViewById(R.id.category);
    holder.source = (TextView) row.findViewById(R.id.source);
    holder.country = (TextView) row.findViewById(R.id.country);
    holder.readytime = (TextView) row.findViewById(R.id.readytime);
    holder.tips = (Button) row.findViewById(R.id.tips);
    holder.fav = (Button) row.findViewById(R.id.fav);

    Recipes ap = data[position];

    imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon);
    holder.txtTitle.setText(ap.getNAME());
    holder.category.setText(ap.getCATEGORY());
    holder.source.setText(ap.getSOURCE());
    holder.country.setText(ap.getCOUNTRY());
    holder.readytime.setText(ap.getREADYTIME());


    return row;
}

static class RecipesHolder {
    ImageView imgIcon;
    TextView txtTitle;
    TextView category;
    TextView source;
    TextView country;
    TextView readytime;
    Button tips;
    Button fav;
}}

//listview_layout.xml

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"><ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:focusable="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="10dp" />

<TextView
    android:id="@+id/readytime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/country"
    android:layout_alignBottom="@+id/country"
    android:layout_marginLeft="73dp"
    android:layout_toRightOf="@+id/country"
    android:focusable="true"
    android:text="TextView"
    android:textColor="#000" />

<TextView
    android:id="@+id/country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/source"
    android:focusable="true"
    android:text="TextView"
    android:textColor="#000" />

<TextView
    android:id="@+id/source"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/country"
    android:layout_alignLeft="@+id/category"
    android:focusable="true"
    android:text="TextView"
    android:textColor="#000" />

<TextView
    android:id="@+id/category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/source"
    android:layout_alignLeft="@+id/title"
    android:text="TextView"
    android:focusable="true"
    android:textColor="#000" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/category"
    android:layout_toRightOf="@+id/imageView1"
    android:text="TextView"
    android:focusable="true"
    android:textColor="#000" />

<Button
    android:id="@+id/fav"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tips"
    android:layout_below="@+id/source"
    android:focusable="true"
    android:background="@drawable/favourite" />

<Button
    android:id="@+id/tips"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginRight="14dp"
    android:background="@drawable/yellow" /></RelativeLayout>
解决方案

finally solved the issue when all the controls(Buttons,textviews) in listview set to focusable false.

这篇关于Onclicklistner不工作的片段列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:15