带有图像、文本和添加项目按钮的自定义 ListView [英] Custom ListView with image, text and add item button

查看:22
本文介绍了带有图像、文本和添加项目按钮的自定义 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 很陌生,无法实现这一点,整天都在搜索.

I am quite new to Android and can't achieve this, been searching all day.

我正在尝试创建的布局.

我创建了自定义 xml 布局,我找到了在创建时添加项目的方法,但我需要列表为空,而不是按下按钮从列表中添加.

I have created the custom xml layout, I have found ways to add items on create, but I need the list to be empty and than when the button is pressed to add from a list.

这是布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow>

<ImageView
android:id="@+id/info_img_view"
android:layout_width="30dp"
        android:layout_height="30dp" />

    <TextView
        android:id="@+id/info_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/info_time_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="right"
        />

</TableRow>

我在主要活动中有一个 ListView:

I have a ListView in the main activity:

<ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

我该怎么做?如果有人能给我指出一个资源,我可以在其中了解代码实际在做什么,而不是我找到的教程,我只是复制和粘贴...

How do I go about this? I would appreciate it if someone can point me to a resource where I can learn what the code is actually doing, not the tutorials I find where I just copy and paste...

谢谢!

编辑,稍微解释一下我想要达到的目标

我有 6 个按钮.当一个按钮被按下时,它应该添加一个包含两个文本视图的列表项,以及总共三个图像中的一个图像.

I have 6 buttons. When a button is pressed it should add a list item with two textviews, and one image out of total three images.

例如,如果 Button1 被按下:添加列表项 > "Text one" "Text one" "imageTwoOfThree".

So for instance if Button1 is pressed: Add list item > "Text one" "Text one" "imageTwoOfThree".

如果Button2被按下:在顶部添加列表项>文本二"文本二"imageTwoOfThree"

Than, if Button2 is pressed: Add list item on top > "Text two" "Text two" "imageTwoOfThree"

等等......文本是硬编码的.

And so on... The text is hardcoded.

推荐答案

这里使用这个:我创建了一个包含 dummydata 的列表,您可以根据自己的需要更改文本和图像

Here use this: I have created a list with dummydata you can change the text and Image according to you

首先创建一个类Data:

First create a class Data:

public class Data {

    private String name,price;
    private int imageId;
    public Data(){}

    public Data(String name,String price,int imageId){
        this.name = name;
        this.price = price;
        this.imageId = imageId;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

然后创建一个 ListView Adapter 来处理您的数据:

Then create a ListView Adapter to handle your data:

public class ListViewAdaptor extends RecyclerView.Adapter<ListViewAdaptor.MyViewHolder> {
    private List<Data> mDataList;

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView name,price;
        public ImageView imageView;

        public MyViewHolder(View view){
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            price= (TextView) view.findViewById(R.id.price);
            imageView = (ImageView) view.findViewById(R.id.image);
        }
    }

    public ListViewAdaptor(List<Data> dataList){
        this.mDataList = dataList;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_view_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Data data = mDataList.get(position);
        holder.name.setText(data.getName());
        holder.price.setText(data.getPrice());
        holder.imageView.setImageResource(data.getImageId());
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
}

您的列表视图项的布局将其命名为 list_view_item:

layout for your list view items name it list_view_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/image"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:text="name"
        android:gravity="center"
        android:textSize="26sp"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/price"
        android:text="price"
        android:gravity="center"
        android:textSize="26sp"
        android:layout_weight="1"/>

</LinearLayout>

然后从您要添加 listView 的活动中在布局中添加 recyclerView:

Then from your activity where you want to add listView add recyclerView in layout:

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view">

    </android.support.v7.widget.RecyclerView>

然后像这样使用这个rec​​yclerview://我已经从我的 MainActivity 调用它,你可以在任何你喜欢的活动中使用它

Then use this recyclerview like this: //I have called it from my MainActivity you can use it in whatever activity you'll like

public class MainActivity extends AppCompatActivity {


    private RecyclerView mRecyclerView;
    private ListViewAdaptor mAdapter;
    private List<Data> mDataList = new ArrayList<>();

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mAdapter = new ListViewAdaptor(mDataList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);
        prepareList();
    }

    public void prepareList(){
       Data data = new Data("Item1","Price1",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item2","Price2",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item3","Price3",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item4","Price4",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item5","Price5",R.drawable.star);
        mDataList.add(data);
    }

}

希望这有帮助!!!

这篇关于带有图像、文本和添加项目按钮的自定义 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆