我在实施RecyclerView时面临一些奇怪的问题
gradle
implementation 'com.android.support:recyclerview-v7:27.1.1'
活动JAVA代码:
RecyclerView rv_agenda = view.findViewById(R.id.rv_agenda);
AgendaAdapter agendaAdapter = new AgendaAdapter(mContext, BaseActivity.mDay1List);
rv_agenda.setLayoutManager(new LinearLayoutManager(mContext));
rv_agenda.getLayoutManager().setMeasurementCacheEnabled(false);
rv_agenda.setAdapter(agendaAdapter);
XML代码:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_agenda"
android:layout_marginTop="@dimen/vertical_spacing"/>
row_itemagenda.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_list_agenda_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraint_agenda"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/img_agenda"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside"
app:srcCompat="@drawable/ic_profileman" />
<ProgressBar
android:id="@+id/agenda_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/text_agenda_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:text="Agenda Titles"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/font_medium"
android:textAllCaps="true"/>
<TextView
android:id="@+id/text_agenda_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/vertical_spacing"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:text="@string/demo_str"
android:textAllCaps="true"
android:textColor="@color/text_caption"
android:textSize="@dimen/font_medium" />
<Button
android:id="@+id/btn_read_more"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="@dimen/vertical_space"
android:layout_marginBottom="4dp"
android:background="@drawable/button_selector"
android:text="@string/read_more"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="@color/white"
android:textSize="@dimen/font_small"
android:textAllCaps="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
适配器代码:
public class Day1AgendaAdapter extends RecyclerView.Adapter<Day1AgendaAdapter.AgendaViewHolder>
{
AgendaActivity mContext;
ArrayList<Day> mList = new ArrayList<>();
public Day1AgendaAdapter(AgendaActivity mContext, ArrayList<Day> mDayList)
{
this.mContext = mContext;
mList = mDayList;
}
@Override
public AgendaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_list_agenda, null);
return new Day1AgendaAdapter.AgendaViewHolder(itemView);
}
@Override
public void onBindViewHolder(final AgendaViewHolder holder, final int position) {
Day data = mList.get(position);
holder.text_agenda_title.setText(data.getTitle());
holder.text_agenda_time.setText(data.getFromTime()+" - "+data.getToTime());
holder.text_agenda_title.setTypeface(mContext.getLightFonts());
holder.text_agenda_time.setTypeface(mContext.getSemiBoldFonts());
holder.btn_read_more.setTypeface(mContext.getRegularFonts());
if(!data.getBackgroundColor().equalsIgnoreCase(""))
{
holder.layout_list_agenda_main.setBackgroundColor(Color.parseColor(data.getBackgroundColor()));
}
else
{
holder.layout_list_agenda_main.setBackgroundColor(Color.parseColor("#ffffff"));
}
if(!data.getDescription().equalsIgnoreCase(""))
{
holder.btn_read_more.setVisibility(View.VISIBLE);
}
else
{
holder.btn_read_more.setVisibility(View.GONE);
}
Glide.with(mContext)
.load(data.getImage())
.error(R.drawable.ic_image_holder)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
holder.agenda_progress.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
holder.agenda_progress.setVisibility(View.GONE);
return false;
}
})
.into(holder.img_agenda);
holder.btn_read_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BaseActivity.AgendaId = BaseActivity.mDay1List.get(position).getAgendaId();
Intent it = new Intent(mContext, AgendaDetailsActivity.class);
mContext.startActivity(it);
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public class AgendaViewHolder extends RecyclerView.ViewHolder {
TextView text_agenda_title,text_agenda_time;
ImageView img_agenda;
ProgressBar agenda_progress;
LinearLayout layout_list_agenda_main;
Button btn_read_more;
public AgendaViewHolder(View convertView) {
super(convertView);
img_agenda = convertView.findViewById(R.id.img_agenda);
agenda_progress = convertView.findViewById(R.id.agenda_progress);
text_agenda_title = convertView.findViewById(R.id.text_agenda_title);
text_agenda_time = convertView.findViewById(R.id.text_agenda_time);
layout_list_agenda_main = convertView.findViewById(R.id.layout_list_agenda_main);
btn_read_more = convertView.findViewById(R.id.btn_read_more);
}
}
}
你知道我在这里做错了吗?
我的议程项目所有子项目都是动态的,它的值标题是多行以及描述,有些项目有按钮可见并且已经消失,所以如何通过制作项目wrap_content来实现期望结果。< / p>
答案 0 :(得分:0)
您的第二个LinearLayout
的方向为水平方向,如果您希望垂直排列ConstraintLayout
和下一个LinearLayout
,则应该是垂直方向。可能还有其他问题,但我会从这开始。