【问题标题】:List Diffrent Text and Images Using ArrayList and RecyclerView使用 ArrayList 和 RecyclerView 列出不同的文本和图像
【发布时间】:2015-03-23 07:02:25
【问题描述】:

我正在尝试使用 RecyclerView 以列表格式列出不同的文本和图像,此时它为所有 5 行显示相同的文本和图像如何使用相同的布局文件在每一行中列出不同的文本和图像和适配器类,或者我必须为我刚接触java的每一行创建一个新的布局+数据列表+适配器类有点困惑。

主要活动

public class MainActivity extends ActionBarActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    ArrayList<DataList> dataList = new ArrayList<DataList>();

    for (int i = 0; i < 5; i ++ ) {

        dataList.add(new DataList(

                "Ball Juggle (while standing)",
                "Hold",
                R.drawable.lt1,
                "+",
                "Tap",
                R.drawable.lt2
        ));
    }

    mAdapter = new MyAdapter(dataList);
    mRecyclerView.setAdapter(mAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

我的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<DataList> dataList;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public View view;
    public ViewHolder(View v) {
        super(v);
        view = v;
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<DataList> dataList) {
    this.dataList = dataList;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_layout, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
   TextView title = (TextView) holder.view.findViewById(R.id.title);
    TextView desc = (TextView) holder.view.findViewById(R.id.desc);
    ImageView imageView = (ImageView) holder.view.findViewById(R.id.imageView);
    TextView desc2 = (TextView) holder.view.findViewById(R.id.desc2);
    TextView desc3 = (TextView) holder.view.findViewById(R.id.desc3);
    ImageView imageView2 = (ImageView) holder.view.findViewById(R.id.imageView2);


    title.setText(dataList.get(position).getTitle());
    desc.setText(dataList.get(position).getDesc());
    imageView.setImageResource(dataList.get(position).getImage());
    desc2.setText(dataList.get(position).getDesc2());
    desc3.setText(dataList.get(position).getDesc3());
    imageView2.setImageResource(dataList.get(position).getImage2());


}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return dataList.size();
}
}

数据列表

public class DataList {

String title;
String desc;
int image;
String desc2;
String desc3;
int image2;

public DataList(String title, String desc, int image, String desc2, String desc3, int image2) {
    this.title = title;
    this.desc = desc;
    this.image = image;
    this.desc2 = desc2;
    this.desc3 = desc3;
    this.image2 = image2;
}

public String getTitle() {
    return title;
}

public String getDesc() {
    return desc;
}

public int getImage() {
    return image;
}

public String getDesc2 () {
    return desc2;
}

public String getDesc3 () {
    return desc3;
}

public int getImage2 () {
    return image2;
}
}

行布局

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

<TextView
    android:paddingRight="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/title"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:paddingLeft="5dp"
    android:paddingRight="2dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/desc"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/title"
    android:layout_toEndOf="@+id/title" />

<ImageView
    android:paddingLeft="2dp"
    android:paddingRight="2dp"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/desc"
    android:layout_toEndOf="@+id/desc" />

<TextView
    android:paddingLeft="2dp"
    android:paddingRight="2dp"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/desc2"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView" />

<TextView
    android:paddingLeft="2dp"
    android:paddingRight="2dp"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/desc3"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/desc2"
    android:layout_toEndOf="@+id/desc2" />

<ImageView
    android:paddingLeft="2dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/desc3"
    android:layout_toEndOf="@+id/desc3" />


</RelativeLayout>

试图这样做,但显然代码是错误的,因为它只显示一行我必须如何编辑此代码

ArrayList<DataList> dataList = new ArrayList<DataList>();

    for (int i = 0; i < 1; i ++ ) {

        dataList.add(new DataList(

                "France",
                "Russia",
                R.drawable.lt1,
                "America",
                "Europe",
                R.drawable.lt2
        ));
    }

    ArrayList<DataList2> dataList2 = new ArrayList<DataList2>();

    for (int i = 0; i < 1; i ++ ) {

        dataList2.add(new DataList2(

                "South Africa",
                "Brazil",
                R.drawable.lt1,
                "New Zeland",
                "Pakistan",
                R.drawable.lt2
        ));
    }

【问题讨论】:

    标签: java android arraylist android-recyclerview


    【解决方案1】:

    为所有 5 行显示相同的文本和图像

    因为这里:

    dataList.add(new DataList(
                    "Ball Juggle (while standing)",
                    "Hold",
                    R.drawable.lt1,
                    "+",
                    "Tap",
                    R.drawable.lt2
            ));
    

    通过在 DataList 类构造函数中传递相同的相同值来创建 DataList 类的所有 5 个对象。

    使用希望在 ListView 行中显示的不同值来创建 DataList 类对象。

    编辑: 这样做:

    1. 创建项目数组:

    String[] strItem = {"Ball Juggle (while standing)","b","c","b","c"};
    int[] intImageIds = {R.drawable.lt1,R.drawable.lt1,...};
    

    2. 在 for 循环中使用 strItemintImageIds 数组创建 DataList 类的对象:

    for (int i = 0; i < 5; i ++ ) {
        dataList.add(new DataList(
                strItem[i],
                "Hold",
                intImageIds[i],
                "+",
                "Tap",
                R.drawable.lt2
        ));
    }
    

    【讨论】:

    • @JacquesKrause:查看我的编辑答案可能会对您有所帮助。让我知道仍然面临任何问题
    • 仍在挣扎我对java非常陌生我想要这个,但显然代码是错误的。见上文
    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多