【问题标题】:Adding buttons to listview android xamarin将按钮添加到listview android xamarin
【发布时间】:2014-03-27 09:15:01
【问题描述】:

我创建了一个标准列表视图,并用文本填充了它。是否可以在每一行添加一个或两个按钮,然后执行某种功能?

【问题讨论】:

  • 我在下面的布局中发布了我的答案,我使用了两个文本视图,而不是您只需在该自定义适配器中添加您设计的布局

标签: android listview button xamarin


【解决方案1】:

是的,您可能需要为该行创建自定义布局, 看到这个 example

【讨论】:

    【解决方案2】:

    您只需在创建 Listview 时使用此自定义适配器,然后您将在此块中编写代码...

    在您的活动中使用此代码...

      CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
      mPullRefreshListView.setAdapter(mAdapter);
    

    然后在这里你替换你的布局......

      public class CustomAdapter extends ArrayAdapter<Sample> {
    
    public ArrayList<Sample> mlist;
    public Context context;
    public LayoutInflater inflater;
    
    public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
        super(context, resource);
        this.mlist = mlist;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getPosition(Sample item) {
        return super.getPosition(item);
    }
    
    @Override
    public Sample getItem(int position) {
        return mlist.get(position);
    }
    
    @Override
    public int getCount() {
        return mlist.size();
    }
    
    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.listitem, null);// Replace your
                                                                // layout....
        TextView text1 = (TextView) convertView.findViewById(R.id.item1);
        TextView text2 = (TextView) convertView.findViewById(R.id.item2);
        text1.setText(mlist.get(position).getListitem1());
        text2.setText(mlist.get(position).getListitem2());
        text2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // you just put your Logic here And use this custom adapter to
                // load your Data By using this particular custom adapter to
                // your listview
                // Change your imageview here
    
            }
        });
        return convertView;
    }
    
    }
    

    【讨论】:

    • 那可以不加按钮吗?你必须使用第二个文本视图吗?
    • 你可以在上面回答。您可以在 xml 中添加按钮,而不是使用 textview(这里是 R.layout.listitem)。或者你可以使用这个theopentutorials.com/tutorials/android/listview/… 教程。您可以添加按钮来代替图像视图。
    • @BrianPeach 您只需使用您自己设计的布局,您将在该列表项中替换...仅此而已
    • @NaveenKumar 这如何与 Xamarin 一起使用,因为 Super 不相关
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多