ListView实现简易版的新闻应用

本文介绍了一个使用ListView而非RecyclerView来展示新闻列表的应用实践。通过创建自定义适配器及Fragment,实现了单击列表项后显示新闻详细内容的功能,适用于不同屏幕尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为之前没学会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再次优化代码,等考完试再说吧尴尬
运行效果如下:界面依旧很丑!!!!!!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值