因为之前没学会RecyclerView怎么用,所以用ListView做了书上关于RecyclerView的Demo
1.新建实体类News类
public class News {
private String Title;
private String Content;
public void setTitle(String Title)
{
this.Title=Title;
}
public void setContent(String Content)
{
this.Content=Content;
}
public String getTitle()
{
return Title;
}
public String getContent()
{
return Content;
}
}
2.新建left_frag.xml布局,用来显示新闻列表
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3.新建right_frag布局,显示新闻内同
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/news_content_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00f"
android:textSize="30dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"/>
<TextView
android:id="@+id/news_content_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000"
android:textSize="25dp"/>
</LinearLayout>
4.新建Right_Fragment,加载刚刚创建的right_frag布局
public class Right_Fragment extends Fragment {
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup contraner, Bundle savedInstanceState)
{
view=inflater.inflate(R.layout.right_frag,contraner,false);//加载right_frag布局
return view;
}
}
5.新建news_item.xml布局,作为listview的子项
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/news_title"
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_weight="1"
android:textSize="20sp" />
</LinearLayout>
6.新建Left_Fragment布局,在里面编写适配器,设置ListView等。
public class Left_Fragment extends Fragment implements ListView.OnItemClickListener {
public boolean isTwoPane;
private List<News> newsList=new ArrayList();
public View onCreateView(LayoutInflater inflater, ViewGroup contrainer, Bundle savvedInstacneState)
{
View view=inflater.inflate(R.layout.left_frag,contrainer,false);
initial();
NewsAdapter adapter=new NewsAdapter(getActivity(),R.layout.news_item,newsList);
ListView news_list_view=(ListView)view.findViewById(R.id.news_list);
news_list_view.setOnItemClickListener(this);//设置监听器
news_list_view.setAdapter(adapter);//设置适配器
return view;
}
public void initial()//初始化数据
{
for(int i=0;i<50;i++)
{
News news=new News();
news.setTitle("the "+i+" news");
news.setContent("this the "+i+" piece of new's content");
newsList.add(news);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News news=newsList.get(position);
if(isTwoPane)//如果双页,则直接找到控件赋值
{
TextView titleText=getActivity().findViewById(R.id.news_content_title);
TextView contentText=getActivity().findViewById(R.id.news_content_content);
titleText.setText(news.getTitle());
contentText.setText(news.getContent());
}else//如果不是双页,则通过Intent赋值
{
Intent intent=new Intent(getActivity(),NewsContentActivity.class);
intent.putExtra("title",news.getTitle());
intent.putExtra("content",news.getContent());
startActivity(intent);
}
}
class NewsAdapter extends ArrayAdapter<News> {//编写适配器
private int resourceId;
public NewsAdapter(Context context, int textResourceId, List<News> objects) {
super(context, textResourceId, objects);
resourceId = textResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
News news = getItem(position);
View view;
ViewHolder viewHolder;
if(convertView!=null)
{
view=convertView;
viewHolder=(ViewHolder)view.getTag();
}else
{
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder=new ViewHolder();
viewHolder.titletext = (TextView) view.findViewById(R.id.news_title);
view.setTag(viewHolder);
}
viewHolder.titletext.setText(news.getTitle());
return view;
}
class ViewHolder{
TextView titletext;
}
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Log.d("result",getActivity().getPackageName());
if(getActivity().findViewById(R.id.right_frag)!=null)
isTwoPane=true;
else
isTwoPane=false;
}
}
7.编写NewsContentActivity类
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.right_frag);
Intent intent=getIntent();
Title=intent.getStringExtra("title");
Content=intent.getStringExtra("content");
TextView titleText=findViewById(R.id.news_content_title);
TextView contentText=findViewById(R.id.news_content_content);
titleText.setText(Title);
contentText.setText(Content);
}
}
8.编写activity_main.xml布局
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_frag"
android:name="com.example.administrator.fragmentpractice.Left_Fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
编写activity_main.xml(sw600dp)
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_frag"
android:name="com.example.administrator.fragmentpractice.Left_Fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/right_frag"
android:name="com.example.administrator.fragmentpractice.Right_Fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
没有用ViewHolder再次优化代码,等考完试再说吧
运行效果如下:界面依旧很丑!!!!!!