Android 在CardView上启用折叠和展开

Android 在CardView上启用折叠和展开,android,android-recyclerview,android-cardview,Android,Android Recyclerview,Android Cardview,我想用CardView(可以折叠和展开)显示一些新闻。默认情况下,不会展开CardView,因此只显示新闻标题。但如果展开,则会显示一个Web视图,显示新闻页面 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/card_pad

我想用CardView(可以折叠和展开)显示一些新闻。默认情况下,不会展开CardView,因此只显示新闻标题。但如果展开,则会显示一个Web视图,显示新闻页面

<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>
以下是我目前的代码:`

<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>
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;
}
}
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);
    }
}
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();
}
}
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;
}
}
<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>
` NewsViewHolder.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;
}
}
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);
    }
}
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();
}
}
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;
}
}
<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>
NewsListAdapter.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;
}
}
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);
    }
}
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();
}
}
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;
}
}
<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>
公共类NewsListAdapter扩展了RecyclerView.Adapter{
私人最终名单新闻名单;
公共新闻列表适配器(列表新闻列表){
this.newsList=新闻列表;
}
@凌驾
公共新闻视图持有者onCreateViewHolder(视图组视图组,int i){
final LayoutInflater LayoutInflater=LayoutInflater.from(viewGroup.getContext());
最终视图v=布局更平坦。充气(R.layout.news\u卡,视图组,假);
返回新的新闻浏览者(v);
}
@凌驾
BindViewHolder上的公共无效(NewsViewHolder,int i){
holder.newsttitle.setText(newsList.get(i.getTitle());
holder.newcontent.loadUrl(newsList.get(i).getUrl());
}
@凌驾
public int getItemCount(){
返回newsList.size();
}
}
NewsFragment.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;
}
}
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);
    }
}
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();
}
}
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;
}
}
<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>
公共类新闻片段扩展片段{
私人回收视图;
公共静态新闻片段newInstance(字符串param1,字符串param2){
NewsFragment=新的NewsFragment();
返回片段;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u news,container,false);
recView=(RecyclerView)view.findViewById(R.id.news\u列表);
setLayoutManager(新的LinearLayoutManager(getActivity());
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(“新闻”);
List dummyNews=newarraylist();
对于(int x=1;x

您可以放置一个按钮,单击该按钮,新闻详细信息的可见性将消失。它将隐藏新闻详细信息并仅显示新闻标题。若要设置版面动画,请使用父视图组(LinearLayout或RelativeLayout或任意)的安卓:animateLayoutChanges=“true”
新闻详细信息文本视图。这将设置文本视图可见性更改的动画。

您可以放置一个按钮,单击该按钮,新闻详细信息可见性将消失。它将隐藏新闻详细信息,并且仅显示新闻标题。要设置版面动画,请使用android:animateLayoutChanges=“true”添加到新闻详细信息文本视图的父视图组(LinearLayout或RelativeLayout或任意)。这将设置文本视图可见性更改的动画

<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>