Android Fragment RecycleListView

1、新建SuperActivity

package com.example.ting.criminalintentpractise;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**
* Created by ting on 17/3/20.
*/

public abstract class SuperFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentlistactivity); //加载activity的样式文件,里面包含一个FragmentLayout
FragmentManager fm=getSupportFragmentManager();  //获取Fragment管理器
Fragment fragment=fm.findFragmentById(R.id.mainfragment); //判断是否被加载过
if (fragment == null){
fragment=createFragment(); //创建对象
fm.beginTransaction().add(R.id.mainfragment,fragment).commit(); //提交管理器
}
}
public abstract Fragment createFragment();
}
2、新建FragmentActivity
public class FragmentListActivity extends SuperFragmentActivity{
@Override
public Fragment createFragment() {
return new StudentListFragment(); //为super创建指向的Fragment
}
}
3、创建Fragment
public class StudentListFragment extends Fragment {
private TextView mNameTextView;
private TextView mAgeTextView;
private List<Student> mStudents;
@Nullable
@Override
    //覆盖onCreateView 方法,返回View.
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.recyclistview, container,false); //绑点Fragment对应的样式文件
RecyclerView recyclerView= (RecyclerView) view.findViewById(R.id.recycleview);  //获取样式文件中的RecycleView
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));  //为RecycleView绑定样式管理器
recyclerView.setAdapter(new RecycleAdpter());              //为RecycleView绑定适配器
return view;                
}
//建立内部类,实现Adapter
private class RecycleAdpter extends RecyclerView.Adapter<RecyclerViewHolder> {
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(getActivity()); //通过LayoutInflater 获取统一的inflater
View v=inflater.inflate(R.layout.studentview,parent,false);  //通过获取的inflater加载 listview 的xml样式
return new RecyclerViewHolder(v);                //返回生成的Holder
}

@Override
  //为每一个listview 注入内容。
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Student student=mStudents.get(position);
mNameTextView.setText(student.getName());
mAgeTextView.setText(student.getAge()+"");
}

@Override
  //获取总的需要显示的数目
public int getItemCount() {
mStudents=StudentList.get();
return mStudents.size();
}
}
//Holder 对生成的listview进行深加工。
private class RecyclerViewHolder extends RecyclerView.ViewHolder{
public RecyclerViewHolder(View itemView) {
super(itemView);
mAgeTextView= (TextView) itemView.findViewById(R.id.age_textview);
mNameTextView= (TextView) itemView.findViewById(R.id.name_textview);
}
}
}



原文地址:https://www.cnblogs.com/swordyt/p/6590437.html