单击项目时更改Recyclerview的颜色(错误)

时间:2017-01-28 09:53:42

标签: android xml android-recyclerview android-cardview

我必须使用回收站视图创建应用程序,但当我点击RecyclerView中的项目时,RecyclerViewCardView的整个颜色会发生变化 我不知道它是怎么发生的我觉得这是一个错误但是没有在错误日志中显示任何错误

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.jhjiko.infouyt.TimelineFragment"
    android:id="@+id/timelineid">

    <!-- TODO: Update blank fragment layout -->

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_datepicker"
        android:background="#84FFFF"
        android:textStyle="bold|italic"
        android:textSize="14sp"
        android:text="Sort by day" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btn_datepicker">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#EFEBE9">
</android.support.v7.widget.RecyclerView>

这是CardView

的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_margin="5dp"
    card_view:cardElevation="5dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="10dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#454E57"
        android:animationCache="false">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#09ADF5"
            android:elevation="1dp">

            <TextView
                android:text="Reportname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_report"
                android:textStyle="bold"
                android:textSize="18sp"
                android:gravity="center"
                android:fontFamily="serif"
                android:layout_gravity="top|center"
                android:textColor="#F9FBE7" />

            <TextView
                android:text="Branchname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_branch"
                android:layout_weight="1"
                android:textSize="14sp"
                android:gravity="right"
                android:layout_gravity="center"
                android:fontFamily="serif"
                android:textStyle="normal|bold"
                android:textColor="#F9FBE7" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:paddingTop="10dp">

            <TextView
                android:text="username"
                android:layout_width="wrap_content"
                android:layout_height="27dp"
                android:textStyle="normal|bold"
                android:textSize="14sp"
                android:id="@+id/txt_user"
                android:fontFamily="serif"
                android:textColor="#F9FBE7" />
            <TextView
                android:id="@+id/textViewdate"
                android:textStyle="bold|italic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.11"
                android:editable="false"
                android:textSize="14sp"
                android:layout_gravity="top"
                android:gravity="right"
                android:text="date"
                android:fontFamily="serif"
                android:textColor="#F9FBE7" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp">

            <TextView
                android:text="Cash        "
                android:layout_height="wrap_content"
                android:layout_width="100dp"
                android:editable="false"
                android:gravity="center"
                android:foregroundGravity="center"
                android:textAlignment="center"
                android:fontFamily="serif"
                android:textStyle="normal|bold"
                android:textColor="#F9FBE7" />

            <TextView
                android:id="@+id/textViewcash"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:editable="false"
                android:gravity="center"
                android:layout_marginLeft="20dp"
                android:fontFamily="serif"
                android:textColor="#F9FBE7" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone">

             <TextView
                android:id="@+id/textViewid"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:editable="false"
                android:gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp">

            <TextView
                android:text="Description"
                android:layout_height="wrap_content"
                android:layout_width="100dp"
                android:editable="false"
                android:gravity="center"
                android:fontFamily="serif"
                android:textStyle="normal|bold"
                android:textAlignment="viewEnd"
                tools:ignore="RtlCompat"
                android:textColor="#F9FBE7" />

            <TextView
                android:id="@+id/textViewdesc"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.02"
                android:focusableInTouchMode="true"
                android:layout_marginLeft="20dp"
                android:inputType="none"
                android:isScrollContainer="true"
                android:minLines="2"
                android:maxLines="10"
                android:scrollbars="vertical"
                android:editable="false"
                android:gravity="center_horizontal|center"
                android:layout_gravity="center"
                android:fontFamily="serif"
                android:textColor="#F9FBE7" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp">

            <com.android.volley.toolbox.NetworkImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imagePhoto"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:adjustViewBounds="false"
                android:cropToPadding="false"
                android:layout_gravity="left|start"
                android:layout_marginRight="5dp" />

            <com.android.volley.toolbox.NetworkImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imagegallery"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:layout_gravity="right|end"
                android:layout_marginLeft="5dp" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

这是我的适配器代码

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.List;

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    private ImageLoader imageLoader, photoLoader;
    private Context context;
    //List of superHeroes
    List<Content> superHeroes;

    public CardAdapter(List<Content> superHeroes, Context context){
        super();
        //Getting all the superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.content_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v,context,superHeroes);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        try {
            Content superHero = superHeroes.get(position);
            imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();

            imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.noimg,R.drawable.noimg));
            photoLoader = CustomVolleyRequest.getInstance(context).getPhotoLoader();
            photoLoader.get(superHero.getPhotoUrl(), ImageLoader.getImageListener(holder.photoView, R.drawable.noimg, R.drawable.noimg));
            if (superHero.getImageUrl().length() > 0) {
                holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
                holder.imageView.setDefaultImageResId(R.drawable.noimg);
                holder.imageView.setErrorImageResId(R.drawable.noimg);
            } else {
                holder.imageView.setDefaultImageResId(R.drawable.noimg);
            }
            if (superHero.getPhotoUrl().length() > 0) {
                holder.photoView.setImageUrl(superHero.getPhotoUrl(), photoLoader);
                holder.photoView.setDefaultImageResId(R.drawable.noimg);
                holder.photoView.setErrorImageResId(R.drawable.noimg);
            } else {
                holder.photoView.setDefaultImageResId(R.drawable.noimg);
            }
            holder.textViewcash.setText(superHero.getName());
            holder.textViewid.setText(String.valueOf(superHero.getReportid()));
            holder.textViewdesc.setText(superHero.getCreatedBy());
            holder.textViewdate.setText(superHero.getFirstAppearance());
            holder.textViewuser.setText(superHero.getUser());
            holder.textViewreport.setText(superHero.getReport());
            holder.textViewbranch.setText(superHero.getBranch());

       }catch (Exception ex){
       }
   }

   @Override
   public int getItemCount() {
       return superHeroes.size();
   }

   class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
       private final List<Content> superHeros;
       public NetworkImageView imageView;
       public NetworkImageView photoView;
       public TextView textViewcash;
       public TextView textViewid;
       public TextView textViewdesc;
       public TextView textViewdate;
       public TextView textViewuser;
       public TextView textViewbranch;
       public TextView textViewreport;
       Context context;
       public ViewHolder(View itemView,Context context,List<Content>superHeroes) {

           super(itemView);
           this.superHeros=superHeroes;
           this.context=context;
           itemView.setOnClickListener(this);
           photoView=(NetworkImageView)itemView.findViewById(R.id.imagegallery);
           imageView = (NetworkImageView) itemView.findViewById(R.id.imagePhoto);
           textViewcash = (TextView) itemView.findViewById(R.id.textViewcash);
           textViewid= (TextView) itemView.findViewById(R.id.textViewid);
           textViewdesc= (TextView) itemView.findViewById(R.id.textViewdesc);
           textViewdate= (TextView) itemView.findViewById(R.id.textViewdate);
           textViewuser=(TextView) itemView.findViewById(R.id.txt_user);
           textViewbranch=(TextView)itemView.findViewById(R.id.txt_branch);
           textViewreport=(TextView)itemView.findViewById(R.id.txt_report);
       }


       @Override
       public void onClick(View v) {
           try {
               int position = getAdapterPosition();
               Content content = this.superHeros.get(position);
               String reportid = String.valueOf(content.getReportid());
               String reportname=content.getReport();
               String url=content.getImageUrl();
               String photo=content.getPhotoUrl();
               Intent intent = new Intent(this.context, showimage.class);
               intent.putExtra("img_id",url);
               intent.putExtra("photo_id",photo);
               intent.putExtra("id", reportid);
               intent.putExtra("report", reportname);
               this.context.startActivity(intent);
           }
           catch (Exception ex){
               ex.printStackTrace();
           }
       }
   }
}

请提前帮助我找到解决此问题的解决方案

1 个答案:

答案 0 :(得分:0)

我认为你已经编写了一个代码来改变“OnClick”事件中Views的颜色。所以,如果你这样做,删除或评论它。要获得明确答案,请分享您的“OnClick”事件代码。