我必须处理一个要求,我必须在另一个下面显示3个滚动表。表将具有多个行和列。它会在水平和垂直方向溢出屏幕。
我写了一个示例项目。但这不符合我的需要。 在当前代码中,每行都单独滚动。我需要在表格中将它们分开并使它们一次滚动,如图所示。 将发布以下代码。
horizontal_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/horizontal_item_text"
android:textColor="@android:color/black"
android:layout_width="100dp"
android:layout_margin="5dp"
android:background="@drawable/bg_white_blue_stroke"
android:padding="5dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="dsfdsfsdf"/>
</LinearLayout>
HorizontalRowAdapter.java
public class HorizontalRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> mDataList;
private int mRowIndex = -1;
public HorizontalRowAdapter() {
}
public void setData(List<String> data) {
if (mDataList != data) {
mDataList = data;
notifyDataSetChanged();
}
}
public void setRowIndex(int index) {
mRowIndex = index;
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView text;
public ItemViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.horizontal_item_text);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View itemView = LayoutInflater.from(context).inflate(R.layout.horizontal_item, parent, false);
ItemViewHolder holder = new ItemViewHolder(itemView);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
ItemViewHolder holder = (ItemViewHolder) viewHolder;
holder.text.setText(mDataList.get(position));
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return mDataList.size();
}
}
vertical_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true">
<TextView
android:id="@+id/item_name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@android:color/holo_blue_bright"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/horizontal_list"
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:visibility="visible"/>
</LinearLayout>
</RelativeLayout>
VerticalListAdapter.java
public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.SimpleViewHolder>{
private final Context mContext;
private static List<SampleData> mData;
private static RecyclerView horizontalList;
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.vertical_item, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
holder.title.setText(mData.get(position).getTitle());
holder.horizontalAdapter.setData(mData.get(position).getTags()); // List of Strings
holder.horizontalAdapter.setRowIndex(position);
}
// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, SampleData data) {
mData.add(position, data);
notifyItemInserted(position);
}
@Override
public int getItemCount() {
return mData.size();
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
private HorizontalRowAdapter horizontalAdapter;
public SimpleViewHolder(View view) {
super(view);
Context context = itemView.getContext();
title = (TextView) view.findViewById(R.id.item_name_tv);
horizontalList = (RecyclerView) itemView.findViewById(R.id.horizontal_list);
horizontalList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
horizontalAdapter = new HorizontalRowAdapter();
horizontalList.setAdapter(horizontalAdapter);
}
}
public VerticalListAdapter(Context mContext, List<SampleData> mData) {
this.mContext = mContext;
this.mData = mData;
}
}
MainActivity.java
public class MainActivity extends Activity {
RecyclerView vertical_list;
VerticalListAdapter verticalListAdapter;
List<SampleData> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vertical_list = (RecyclerView) findViewById(R.id.vertical_list);
vertical_list.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
vertical_list.setLayoutManager(llm);
prepareData();
verticalListAdapter = new VerticalListAdapter(this,mData);
vertical_list.setAdapter(verticalListAdapter);
}
private void prepareData(){
mData = new ArrayList<>();
for(int i = 0; i < 20; i ++){
SampleData sampleData = new SampleData();
sampleData.setTitle("Position = "+i);
List<String> tags = new ArrayList<>();
for(int j = 0; j <= i; j ++){
tags.add("Inner Position = "+j);
}
sampleData.setTags(tags);
mData.add(sampleData);
}
}
}
感谢任何帮助或建议。 谢谢