问题描述
列出线框.我正在尝试建立一个列表,例如图片中的一个列表,在该列表中,我具有一个水平的RecyclerView并将ListViews(或RecyclerViews)作为项目.你能给我一个关于如何构建它的例子吗?
List wireframe.I'm trying to build a list like the one in the picture where I have a horizontal RecyclerView with ListViews (or RecyclerViews) as items. Can you give me an example on how to build this?
推荐答案
尝试一下:-
MainActivity.java
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static RecyclerViewAdapter adapter;
private LinearLayoutManager layoutManager;
private static RecyclerView recyclerView;
private List<Book> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getNextItems();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new RecyclerViewAdapter(data, this, recyclerView);
recyclerView.setAdapter(adapter);
}
private void getNextItems() {
int itemCount = data.size();
for (int i = itemCount; i <= itemCount + 5; i++) {
Book book = new Book();
book.title = "Title " + i;
book.author = "Author " + i;
book.description = "Description " + i;
data.add(book);
}
}
}
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="12dp"
tools:context="com.example.sabari.recyclerviewsample.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
RecyclerViewAdapter.java
RecyclerViewAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by sabari on 2/8/2016.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter {
public static final String Name = "Name";
public static final String Author = "Author";
public static final String Description = "Description";
private List<Book> dataSet;
private Context context;
private final int VIEW_ITEM = 1;
// The minimum amount of items to have below your current scroll position before loading more.
public RecyclerViewAdapter(List<Book> data, Context context, RecyclerView recyclerView) {
this.dataSet = data;
this.context = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ListView listView;
public MyViewHolder(View itemView) {
super(itemView);
listView = (ListView) itemView.findViewById(R.id.listview);
}
}
@Override
public int getItemCount() {
return dataSet.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_layout, parent, false);
vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
// Binding Views...
ArrayList<HashMap> list = new ArrayList<HashMap>();
for (Book book : dataSet) {
HashMap hashMap = new HashMap();
hashMap.put(Name, book.title);
hashMap.put(Author, book.author);
hashMap.put(Description, book.description);
list.add(hashMap);
}
ListViewAdapter adapter = new ListViewAdapter(context, list);
((MyViewHolder) holder).listView.setAdapter(adapter);
}
}
}
ListViewAdapter.java(参考: http://www.technotalkative.com/android-多列列表视图)
ListViewAdapter.java (Ref: http://www.technotalkative.com/android-multi-column-listview)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by sabari on 2/13/2016.
*/
public class ListViewAdapter extends BaseAdapter {
public static final String Name = "Name";
public static final String Author = "Author";
public static final String Description = "Description";
public ArrayList<HashMap> list;
Context context;
public ListViewAdapter(Context context, ArrayList<HashMap> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView name;
TextView author;
TextView description;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.author = (TextView) convertView.findViewById(R.id.author);
holder.description = (TextView) convertView.findViewById(R.id.description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
HashMap map = list.get(position);
holder.name.setText(map.get(Name).toString());
holder.author.setText(map.get(Author).toString());
holder.description.setText(map.get(Description).toString());
return convertView;
}
}
Book.java
Book.java
public class Book {
String title;
String author;
String description;
}
book_layout.xml
book_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="@drawable/border"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<ListView
android:id="@+id/listview"
android:layout_width="250dp"
android:layout_height="match_parent" />
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="@+id/author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center" />
</LinearLayout>
自定义可绘制对象-border.xml
Custom drawable - border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#2B91B4" />
</shape>
这篇关于带有ListViews的Android Horizontal RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!