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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值