1、定义两个Fragment,加载布局与Activity类似,leftFragment展示标题列表,点击列表,RightFragment展示详情
public class RightFragment extends Fragment {
private TextView title;
private TextView content;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
title = view.findViewById(R.id.content_title);
content = view.findViewById(R.id.content_content);
return view;
}
public void refresh(String title, String content) {
this.title.setText(title);
this.content.setText(content);
}
}
public class LeftFragment extends Fragment {
public LeftFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
RecyclerView recyclerView = view.findViewById(R.id.newsleft);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(new TitleListAdapter(initData()));
return view;
}
private List<News> initData() {
List<News> list = new ArrayList<>();
for (int i = 0;i< 10;i++){
News news = new News();
news.setTitle("title" + i);
StringBuilder builder = new StringBuilder();
int max = new Random().nextInt(100);
for (int j = 0;j<max;j++) {
builder.append(j);
}
news.setContent(builder.toString());
list.add(news);
}
return list;
}
class TitleListAdapter extends RecyclerView.Adapter<TitleHolder> {
private List<News> list;
public TitleListAdapter(List<News> list) {
this.list = list;
}
@NonNull
@Override
public TitleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
TitleHolder holder = new TitleHolder(LayoutInflater.from(getContext()).inflate(R.layout.news_title_layout, parent, false));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
News news = list.get(holder.getAdapterPosition());
RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.rightfragment);
rightFragment.refresh(news.getTitle(), news.getContent());
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull TitleHolder holder, int position) {
News news = list.get(position);
holder.textView.setText(news.getTitle());
}
@Override
public int getItemCount() {
return list.size();
}
}
class TitleHolder extends RecyclerView.ViewHolder{
private TextView textView;
public TitleHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.newstitle);
}
}
}
2、Activity引用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".four.NewsActivity">
<fragment
android:id="@+id/leftfragment"
android:layout_width="0dp"
android:name="com.example.firstlinecode.four.LeftFragment"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/rightfragment"
android:layout_width="0dp"
android:name="com.example.firstlinecode.four.RightFragment"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
public class NewsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
}
private void replace(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.leftfragment, fragment);
transaction.commit();
}
}