Android————一个简单的新闻面板

功能实现:
使用Fragment碎片实现横竖两个碎片,达到一个简易新闻界面的效果
效果图如下:
Android————一个简单的新闻面板_第1张图片
第一步:建一个实体类News
代码如下:

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;
}
}
第二步:新建一个活动NewsContent
代码如下:
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);
}
}
布局文件代码如下:



第三步:建立一个子布局content_show,用做新闻内容布局
代码如下:



    

    

    


第四步:建立NewsContentFragment和NewsTitleFragment用做新闻内容布局
两个都继承Fragment,其中NewsTitleFragment建立了一个RecyclerView内部类用作适配器

NewsContentFragment代码如下:

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 getNews() {
List newsList = new ArrayList();
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 {
private List mNewsList;
public NewsAdapter(List 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);
}
}
}
}
最后建立一个适用于RecyclerView的子布局命名为new_item

代码如下:

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

    

你可能感兴趣的:(Android,Studio,android,java)