1. 创建 NewsItem 类:
public class NewsItem {
private String title;
private String content;
public NewsItem(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
2. 创建 NewsListAdapter 类:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class NewsListAdapter extends ArrayAdapter<NewsItem> {
public NewsListAdapter(@NonNull Context context, int resource, @NonNull List<NewsItem> newsList) {
super(context, resource, newsList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.news_list_item, parent, false);
}
NewsItem newsItem = getItem(position);
if (newsItem != null) {
TextView titleTextView = convertView.findViewById(R.id.titleTextView);
TextView contentTextView = convertView.findViewById(R.id.contentTextView);
titleTextView.setText(newsItem.getTitle());
contentTextView.setText(newsItem.getContent());
}
return convertView;
}
}
3. 创建新闻列表项的布局文件:res/layout/news_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
</LinearLayout>
4. 创建 NewsListFragment 类:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;
public class NewsListFragment extends Fragment {
private ListView listView;
private List<NewsItem> newsItemList;
private OnItemSelectedListener listener;
public interface OnItemSelectedListener {
void onItemSelected(NewsItem newsItem);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news_list, container, false);
listView = view.findViewById(R.id.listView);
// 初始化新闻数据
newsItemList = new ArrayList<>();
newsItemList.add(new NewsItem("新闻标题1", "新闻内容1"));
newsItemList.add(new NewsItem("新闻标题2", "新闻内容2"));
newsItemList.add(new NewsItem("新闻标题3", "新闻内容3"));
// 创建并设置适配器
NewsListAdapter adapter = new NewsListAdapter(getActivity(), R.layout.news_list_item, newsItemList);
listView.setAdapter(adapter);
// 设置列表项点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listener != null) {
listener.onItemSelected(newsItemList.get(position));
}
}
});
return view;
}
public void setOnItemSelectedListener(OnItemSelectedListener listener) {
this.listener = listener;
}
}
5. 创建新闻列表 Fragment 的布局文件:res/layout/fragment_news_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在你的主 Activity 中,你可以使用 NewsListFragment,并在需要的时候替换或添加到布局中。例如:
public class MainActivity extends AppCompatActivity implements NewsListFragment.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewsListFragment newsListFragment = new NewsListFragment();
newsListFragment.setOnItemSelectedListener(this);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, newsListFragment)
.commit();
}
@Override
public void onItemSelected(NewsItem newsItem) {
// 处理选定的新闻项,可以在这里启动新的 Fragment 或 Activity 来显示新闻详情
// 例如:显示新闻详情的 NewsDetailFragment
}
}
这是一个简单的例子,你可以根据你的需求进行修改和扩展。例如,你可以添加更多的数据字段到 NewsItem 类,或者创建一个新的 Fragment 来显示选中新闻的详细信息。
转载请注明出处:http://www.pingtaimeng.com/article/detail/15186/Android