我尝试通过点击来更改ImageView
的背景,但它不起作用。
我在ViewHolder中有这个
我得到了
尝试调用虚方法 空对象erference上的..StreamViewHolder.getAdapterPosition()
import java.util.List;
import de.caglar.aqua.R;
import de.caglar.aqua.objects.StreamObject;
import de.caglar.aqua.views.CircularImageView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class StreamAdapter extends RecyclerView.Adapter<StreamAdapter.StreamViewHolder> implements View.OnClickListener {
public static List<StreamObject> streams;
public StreamAdapter(List<StreamObject> s){
streams = s;
}
public static class StreamViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
public TextView streamTitle;
public TextView streamText;
public TextView streamDescription;
public CircularImageView streamPhoto;
public ImageView streamLike;
public StreamViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.stream_cardview);
streamTitle = (TextView) itemView.findViewById(R.id.stream_title);
streamText = (TextView) itemView.findViewById(R.id.stream_text);
streamDescription = (TextView) itemView.findViewById(R.id.stream_description);
streamPhoto = (CircularImageView) itemView.findViewById(R.id.stream_photo);
streamLike = (ImageView) itemView.findViewById(R.id.stream_like);
}
}
@Override
public int getItemCount() {
return streams.size();
}
@Override
public void onBindViewHolder(StreamViewHolder streamViewHolder, int i) {
streamViewHolder.streamTitle.setText(streams.get(i).title);
streamViewHolder.streamText.setText(streams.get(i).text);
streamViewHolder.streamDescription.setText(streams.get(i).description);
streamViewHolder.streamPhoto.setBackground(streams.get(i).photo);
if(streams.get(i).isLiked) {
streamViewHolder.streamLike.setImageResource(R.drawable.ic_action_like_active);
} else {
streamViewHolder.streamLike.setImageResource(R.drawable.ic_action_like);
}
streamViewHolder.streamLike.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
if (v instanceof ImageView) {
StreamViewHolder holder = (StreamViewHolder) v.getTag();
int position = holder.getAdapterPosition();
if(streams.get(position).isLiked) {
streams.get(position).isLiked = false;
holder.streamLike.setImageResource(R.drawable.ic_action_like);
} else {
streams.get(position).isLiked = true;
holder.streamLike.setImageResource(R.drawable.ic_action_like_active);
}
}
} catch (Exception e) {
Toast.makeText(v.getContext(), e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public StreamViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_stream, viewGroup, false);
StreamViewHolder pvh = new StreamViewHolder(v);
return pvh;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
答案 0 :(得分:0)
将其放入<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_gravity="center"
android:src="@drawable/rosezwei" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"/>
</LinearLayout>
方法
onBindViewHolder