package com.example.haha.note;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by haha on 2017/9/8.
*/
public class NotesTitleFragment extends Fragment {
private boolean isTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.notes_title_frag,container,false);
RecyclerView noteTitleRecyclerView=(RecyclerView) view.findViewById(R.id.notes_title_recycler_view);
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
noteTitleRecyclerView.setLayoutManager(layoutManager);
NoteAdapter adapter=new NoteAdapter(getNote());
noteTitleRecyclerView.setAdapter(adapter);
return view;
}
private List<notes> getNote(){
List<notes> notesList=new ArrayList<>();
for(int i=1;i<=50;i++){
notes note=new notes();
note.setTitle("This is title" + i);
note.setContent(getRandomLengthContent("This is note content"+i+"."));
notesList.add(note);
}
return notesList;
}
private String getRandomLengthContent(String content){
Random random=new Random();
int length=random.nextInt(20)+1;
StringBuilder builder=new StringBuilder();
for(int i=0;i<length;i++){
builder.append(content);
}
return builder.toString();
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.notes_content_layout) !=null){
isTwoPane=true;
}
else isTwoPane=false;
}
class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
private List<notes> mNoteList;
class ViewHolder extends RecyclerView.ViewHolder{
TextView noteTitleText;
public ViewHolder(View view){
super(view);
noteTitleText=(TextView) view.findViewById(R.id.note_title);
}
}
public NoteAdapter(List<notes> notesList){
mNoteList=notesList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_item,parent,false);
final ViewHolder holder=new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notes note=mNoteList.get(holder.getAdapterPosition());
if(isTwoPane){
NotesContentFragment notesContentFragment=(NotesContentFragment) getFragmentManager().findFragmentById(R.id.notes_content_fragment);
notesContentFragment.refresh(note.getTitle(),note.getContent());
}else {
notesContentActivity.actionStart(getActivity(),note.getTitle(),note.getContent());
}
}
});
return holder;
}
public void onBindViewHolder(ViewHolder holder,int position){
notes note=mNoteList.get(position);
holder.noteTitleText.setText(note.getTitle());
}
public int getItemCount(){
return mNoteList.size();
}
}
}试过网上说得很多办法都没解决问题,小白求教
蜂之谷
相关分类