赞
踩
功能实现:
使用Fragment碎片实现横竖两个碎片,达到一个简易新闻界面的效果
效果图如下:
public class News {
private String Title;//标题
private String Content;//内容
public News(String Title, String Content){
this.Title = Title;
this.Content = Content;
}
public String getTitle() {
return Title;
}
public String getContent() {
return Content;
}
}
public class NewsContent extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news_content); String newsTitle = getIntent().getStringExtra("news_title"); String newsContent = getIntent().getStringExtra("news_content"); NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentById(R.id.news_content_fragment); newsContentFragment.Refresh(newsTitle,newsContent); } public static void ActionStart(Context context, String newsTitle, String newsContent ){ Intent intent = new Intent(context,NewsContent.class); intent.putExtra("news_title",newsTitle); intent.putExtra("news_content",newsContent); context.startActivity(intent); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/news_content_fragment" android:name="com.example.newsinterface.NewsContentFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> <font size =4 color = "#ff0000">第三步:建立一个子布局content_show,用做新闻内容布局</font> 代码如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/liner_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="invisible"> <TextView android:id="@+id/NewsTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:textSize="20sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"/> <TextView android:id="@+id/NewsContent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="15dp" android:textSize="18sp"/> </LinearLayout> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="#000"/> </RelativeLayout>
public class NewsContentFragment extends Fragment { private View view; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.content_show,container,false); return view; } public void Refresh(String newsTitle,String newsContent){ View visibilityLayout = view.findViewById(R.id.liner_layout); visibilityLayout.setVisibility(View.VISIBLE);//显示 TextView newsTitleText = (TextView) view.findViewById(R.id.NewsTitle); TextView newsContentText = (TextView) view.findViewById(R.id.NewsContent); newsTitleText.setText(newsTitle); newsContentText.setText(newsContent); } } NewsTitleFragment代码如下: public class NewsTitleFragment extends Fragment { private boolean flag; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.news_title, container, false); RecyclerView newsTitleRecyclerView = (RecyclerView) view. findViewById(R.id.news_title_recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); newsTitleRecyclerView.setLayoutManager(layoutManager); NewsAdapter adapter = new NewsAdapter(getNews()); newsTitleRecyclerView.setAdapter(adapter); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getActivity().findViewById(R.id.news_content_fragment) != null) flag = true; else flag = false; } private List<News> getNews() { List<News> newsList = new ArrayList<News>(); for (int i = 0; i <= 50; ++i) { News news = new News("This is news title " + i, "This is news Content " + i + ". "); newsList.add(news); } return newsList; } private String getRandomLengthContent(String s) { Random random = new Random(); int length = random.nextInt(20) + 1; StringBuilder builder = new StringBuilder(); for (int i = 0; i < length; ++i) { builder.append(s); } return builder.toString(); } class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> { private List<News> mNewsList; public NewsAdapter(List<News> newsList) { mNewsList = newsList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()). inflate(R.layout.news_item, parent, false); final ViewHolder holder = new ViewHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { News news = mNewsList.get(holder.getAdapterPosition()); if (flag) { NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment); newsContentFragment.Refresh(news.getTitle(), news.getContent()); news.getContent(); } else { NewsContent.ActionStart(view.getContext(), news.getTitle(), news.getContent()); } } }); return holder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { News news = mNewsList.get(position); holder.newsTitleText.setText(news.getTitle()); } @Override public int getItemCount() { return mNewsList.size(); } class ViewHolder extends RecyclerView.ViewHolder { TextView newsTitleText; public ViewHolder(@NonNull View itemView) { super(itemView); newsTitleText = (TextView) itemView.findViewById(R.id.news_title); } } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news_title_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。