我想使用CardView显示一些新闻(可以折叠和展开)。默认情况下,CardView不会展开,因此仅显示新闻标题。但如果展开,可以看到WebView,显示新闻页面。
到目前为止,这些是我的代码:`
News.java
public class News {
private String title;
private String url;
public News(){
this("", "");
}
public News(String title, String url){
this.title = title;
this.url = url;
}
public void setTitle(String title){
this.title = title;
}
public void setUrl(String url){
this.url = url;
}
public String getTitle(){
return title;
}
public String getUrl(){
return url;
}
}
` 的 NewsViewHolder.java
public class NewsViewHolder extends RecyclerView.ViewHolder {
public TextView newsTitle;
public WebView newsContent;
public NewsViewHolder(View itemView){
super(itemView);
newsTitle = (TextView) itemView.findViewById(R.id.news_title);
newsContent = (WebView) itemView.findViewById(R.id.news_web_content);
}
}
NewsListAdapter.java
public class NewsListAdapter extends RecyclerView.Adapter<NewsViewHolder> {
private final List<News> newsList;
public NewsListAdapter(List<News> newsList) {
this.newsList = newsList;
}
@Override
public NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
final View v = layoutInflater.inflate(R.layout.news_card, viewGroup, false);
return new NewsViewHolder(v);
}
@Override
public void onBindViewHolder(NewsViewHolder holder, int i) {
holder.newsTitle.setText(newsList.get(i).getTitle());
holder.newsContent.loadUrl(newsList.get(i).getUrl());
}
@Override
public int getItemCount() {
return newsList.size();
}
}
NewsFragment.java
public class NewsFragment extends Fragment {
private RecyclerView recView;
public static NewsFragment newInstance(String param1, String param2){
NewsFragment fragment = new NewsFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
recView = (RecyclerView) view.findViewById(R.id.news_list);
recView.setLayoutManager(new LinearLayoutManager(getActivity()));
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("News");
List<News> dummyNews = new ArrayList<News>();
for (int x = 1; x <= 5; x++){
News news = new News("News #"+x, "file:///android_asset/www/dummy.html");
dummyNews.add(news);
}
recView.setAdapter(new NewsListAdapter(dummyNews));
return view;
}
}
`
news_card.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/card_padding"
tools:background="#22000000">
<TextView
android:id="@+id/news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/default_margin_padding"
android:textSize="@dimen/quiz_question_text_size" />
<WebView
android:id="@+id/news_web_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- dummy sharing buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="16sp"
android:text="WhatsApp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="16sp"
android:text="E-mail"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="16sp"
android:text="Facebook"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>
所有新闻都正确显示。现在的问题是:
怎么办?或者是否有符合我要求的第三方CardView库?谢谢。
答案 0 :(得分:1)
您可以放置一个按钮,点击该按钮,我可以看到GONE的新闻详细信息。它将隐藏新闻详细信息,仅显示新闻标题。要使布局动画,请使用> row.names(mydataframe)
[1] "1_S1_annotated_filtered.vcf.gz1" "1_S1_annotated_filtered.vcf.gz2" "1_S1_annotated_filtered.vcf.gz3" "1_S1_annotated_filtered.vcf.gz6"
[5] "1_S1_annotated_filtered.vcf.gz7" "1_S1_annotated_filtered.vcf.gz8"
...
[457] "6_S6_annotated_filtered.vcf.gz877" "6_S6_annotated_filtered.vcf.gz888" "6_S6_annotated_filtered.vcf.gz907" "7_S7_annotated_filtered.vcf.gz309"
[461] "7_S7_annotated_filtered.vcf.gz354" "7_S7_annotated_filtered.vcf.gz477" "7_S7_annotated_filtered.vcf.gz485" "7_S7_annotated_filtered.vcf.gz537"
[465] "7_S7_annotated_filtered.vcf.gz569" "7_S7_annotated_filtered.vcf.gz575" "7_S7_annotated_filtered.vcf.gz721" "7_S7_annotated_filtered.vcf.gz871"
[469] "7_S7_annotated_filtered.vcf.gz892" "8_S8_annotated_filtered.vcf.gz136" "8_S8_annotated_filtered.vcf.gz191" "8_S8_annotated_filtered.vcf.gz967"
到新闻详细信息文本视图的父ViewGroup(LinearLayout或RelativeLayout或任何)。这将为textView的可见性更改设置动画。