在我开始之前,我在思考是否可以使用viewpager显示带有一些textview和recycleview的布局? 类似下面的布局,我想用viewpager显示它。有可能吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dynamic"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Large Text"
android:textStyle="bold"
android:layout_marginTop="20px"
android:layout_marginLeft="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:layout_marginLeft="20px"
android:text="New Text" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:layout_marginLeft="20px"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
好吧......我在我现有的项目上尝试过它并且它已经粉碎了,我不知道为什么......
示例代码如下
我调用viewpager适配器的类:
public class Details_page extends AppCompatActivity {
ViewPager viewPager;
CustomSwipAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
android.support.v7.app.ActionBar actionBar= getSupportActionBar();
if(actionBar!=null)
{
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
Bundle bundle=getIntent().getExtras();
String[] array=bundle.getStringArray("s");
int point=Integer.parseInt(array[0]);
String title_head=array[2];
setTitle(title_head);
String catagory=array[1];
Cursor cursor;
cursor=getdata(getApplicationContext(),catagory);
viewPager=(ViewPager)findViewById(R.id.view_pager);
adapter =new CustomSwipAdapter(this,cursor,point);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(point);
}
public Cursor getdata(Context context,String catagory)
{
Db_helper db_helper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
db_helper = new Db_helper(context);
sqLiteDatabase = db_helper.getReadableDatabase();
cursor = db_helper.getallInformations(sqLiteDatabase, catagory);
cursor.moveToFirst();
db_helper.close();
return cursor;
}
}
viewpager adapter
public class CustomSwipAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Cursor cursor;
private int point;
private RecyclerView recyclerView;
private IELTS_detail_row_adapter adapter;
private RecyclerView.LayoutManager layoutManager;
public CustomSwipAdapter(Context context,Cursor cursor,int point)
{
this.context=context;
this.cursor=cursor;
this.point=point;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
List<Mentor_detail_row_info> data=new ArrayList<>();
layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swip_layout,container,false);
TextView textView=(TextView)item_view.findViewById(R.id.page_title);
TextView textView1=(TextView)item_view.findViewById(R.id.info_text);
recyclerView =(RecyclerView)item_view.findViewById(R.id.recycler_detail);
recyclerView.addItemDecoration(new DividerItemDecoration(context, null, false, true));
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
textView1.setMovementMethod(new ScrollingMovementMethod());
cursor.moveToPosition(position);
textView.setText(cursor.getString(1));
textView1.setText(cursor.getString(3));
String[]links=cursor.getString(3).split(";");
for(int i=0;i<=links.length;i++)
{
Mentor_detail_row_info current=new Mentor_detail_row_info();
if(links[i]!=null)
{
current.link=links[i];
data.add(current);
}
}
adapter=new IELTS_detail_row_adapter(context,data);
recyclerView.setAdapter(adapter);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
和recycleview adapter
public class IELTS_detail_row_adapter extends RecyclerView.Adapter<IELTS_detail_row_adapter.Mentor_menu_viewHolder> {
private LayoutInflater inflater;
List<Mentor_detail_row_info> data = Collections.emptyList();
Context context;
private ClickListener clickListener;
public IELTS_detail_row_adapter(Context context, List<Mentor_detail_row_info> data) {
this.context=context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public Mentor_menu_viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row_detail, parent, false);
Mentor_menu_viewHolder holder = new Mentor_menu_viewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(Mentor_menu_viewHolder holder, int position) {
Mentor_detail_row_info current = data.get(position);
holder.link.setText(current.link);
}
public void setClickListener(ClickListener clickListener)
{
this.clickListener=clickListener;
}
@Override
public int getItemCount() {
return data.size();
}
class Mentor_menu_viewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView link;
public Mentor_menu_viewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
link = (TextView) itemView.findViewById(R.id.row_list_link);
}
@Override
public void onClick(View v) {
Mentor_detail_row_info current=data.get(getAdapterPosition());
clickListener.ItemClick(v,current.link);
}
}
public interface ClickListener{
public void ItemClick(View view,String link);
}
}
答案 0 :(得分:0)
是的。
您可以在您调用的片段中将Recyclerview设置为ViewPager的页面。当ViewPager加载片段时,recyclerviews将实例化并填充。
缺点:您可能会遇到水平滑动(viewpager)和垂直滑动(recyclerview)之间的延迟。
如果您需要更具体的答案,请提出更具体的问题。
答案 1 :(得分:0)
您可以将FragmentPagerAdapter
用于viewPager,并将recyclerView添加到您的片段中。以此来源为例 - https://github.com/chrisbanes/cheesesquare