我想在使用TabLayout(片段)的Activity中组合Recyclerview,以便我可以在不同的列表之间滑动。然而到目前为止,我只实现了ListActivity或标签活动(android studio中的标准活动),但不是两者。如果有人可以提供帮助,我会很高兴。
AdapterFish.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.TextView;
import java.util.Collections;
import java.util.List;
public class AdapterFish extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFish> data= Collections.emptyList();
DataFish current;
int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public AdapterFish(Context context, List<DataFish> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.tab2, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
DataFish current=data.get(position);
myHolder.textmatkul.setText(current.matkul);
myHolder.textwaktu_batal.setText("Tanggal Batal: " + current.waktu_batal);
myHolder.textwaktu_pengganti.setText("Tanggal Pengganti: " + current.waktu_pengganti);
myHolder.textdosen.setText("Nama Dosen: " + current.dosen);
myHolder.textruang.setText("Ruang: " + current.ruang);
myHolder.textalasan.setText("Alasan: " + current.alasan);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView textmatkul;
TextView textwaktu_batal;
TextView textwaktu_pengganti;
TextView textdosen;
TextView textruang;
TextView textalasan;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
textmatkul= (TextView) itemView.findViewById(R.id.textmatkul);
textwaktu_batal = (TextView) itemView.findViewById(R.id.textwaktu_batal);
textwaktu_pengganti = (TextView) itemView.findViewById(R.id.textwaktu_pengganti);
textdosen = (TextView) itemView.findViewById(R.id.textdosen);
textalasan = (TextView) itemView.findViewById(R.id.textalasan);
textruang = (TextView) itemView.findViewById(R.id.textruang);
}
}
}
Tab2.java :
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Lamberto on 03/04/2017.
*/
public class Tab2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2, container, false);
}
}
答案 0 :(得分:1)
请尝试按以下步骤操作:
activity_main.xml
,Toolbar
和TabLayout
ViewPager
RecyclerView
内容展示的片段设计带有Tab
的XML格式。FragmentPagerAdapter
填充Fragments
ViewPager
FragmentPagerAdapter
ViewPager
设置为ViewPager.setAdapter(FragmentPagerAdapter)
TabLayout.setupWithViewPager(ViewPager)
授权ViewPager
使用TabLayout
功能。Fragment
,请创建RecyclerViewAdapter
以将数据填充到RecyclerView
这是一个简单的tutorial。希望这会有所帮助〜