我使用 constarintLayout 作为我recycleView
的容器,并依赖于json(检索数据)来填充我的项目。
所以我在这里做了一些技巧,如果我检索到的 json 包含特定值,我必须在约束布局中制作一些项目以消失并调整其他项目的大小。
一切正常,但是当我向下滚动并向上滚动时,改变后的价值回到了我不需要的旧雕像。
这是我的constraintLayout.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:clickable="true"
android:focusable="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
app:layout_constraintLeft_toRightOf="@+id/article_subtitle"
tools:layout_editor_absoluteY="27dp"
android:id="@+id/containerDetailsItem">
<TextView
android:id="@+id/technicalName"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="141dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:maxLines="1"
android:paddingLeft="5dp"
android:textColor="@color/white"
android:textSize="@dimen/text_main"
app:layout_constraintRight_toLeftOf="@+id/thumbnailLayout"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/technicalProfile"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:drawableRight="@drawable/account_circle"
android:gravity="center"
android:maxLines="1"
android:text="الملف الشخصي"
android:padding="4dp"
android:textColor="@color/background"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintHorizontal_weight=".1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/technicalAge"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/technicalExperince"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginRight="19dp"
android:layout_marginTop="0dp"
android:maxLines="1"
android:textColor="@color/white"
app:layout_constraintRight_toLeftOf="@+id/thumbnailLayout"
app:layout_constraintTop_toBottomOf="@+id/technicalExperince" />
<TextView
android:id="@+id/technicalAge"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginRight="17dp"
android:layout_marginTop="2dp"
android:foregroundGravity="right"
android:gravity="right"
android:maxLines="1"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/technicalExperince"
app:layout_constraintHorizontal_weight=".2"
app:layout_constraintRight_toLeftOf="@+id/thumbnailLayout"
app:layout_constraintTop_toBottomOf="@+id/technicalName"
app:layout_constraintVertical_bias="0.0" />
<RelativeLayout
android:id="@+id/thumbnailLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-36dp"
android:layout_marginRight="-1dp"
android:paddingBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/thumbnail"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:background="@drawable/unselected"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
这里是布局项目中更改的示例:
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("2")) {
holder.ageView.setTypeface(font);
holder.ageView.setText("\uf0ce" + " office" );
holder.experinceView.setVisibility(View.GONE);
holder.nameView.setTextSize(23);
//holder.constraintView.setBackgroundColor(getResources().getColor(R.color.office_color));
}
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("1")) {
holder.ageView.setTypeface(font);
holder.ageView.setText( "\uf19c" + " company" );
holder.experinceView.setVisibility(View.GONE);
holder.nameView.setTextSize(23);
//holder.constraintView.setBackgroundColor(getResources().getColor(R.color.company_color));
}
所以,只有当检索到的type
数据等于特定值时,才编辑此数据。
如果type
不等于特定type
,我会让它显示为正常形状。
我的适配器:
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
public Adapter(Cursor cursor) {
mCursor = cursor;
}
@Override
public long getItemId(int position) {
mCursor.moveToPosition(position);
return mCursor.getLong(ArticleLoader.Query._ID);
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.list_item_profiles_vertical, parent, false);
final ViewHolder vh = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW,
ItemsContract.Items.buildItemUri(getItemId(vh.getAdapterPosition()))));
try {
int i = Integer.parseInt((String) mCursor.getString(ArticleLoader.Query.SERVER_ID));
Log.i(TAG, "bindViews: " + i);
i = i + 1;
Config.BASE_URL = new URL("http:/json/users?user=" + mCursor.getString(ArticleLoader.Query.SERVER_ID));
startService(new Intent(DisplaysActivity.this, CurrentService.class));
Log.i(TAG, "onCreateDisplay: " + mCursor.getString(ArticleLoader.Query.SERVER_ID));
Log.i(TAG, "onClick: " + Config.BASE_URL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.ageView.setTypeface(typeface);
mCursor.moveToPosition(position);
holder.nameView.setText(mCursor.getString(ArticleLoader.Query.NAME));
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("2")) {
holder.profileIcon.setTypeface(font);
holder.ageView.setTypeface(font);
holder.ageView.setText(" مكتب" );
holder.ageView.setTextSize(17);
holder.profileIcon.setText("\uf0f7");
holder.experinceView.setVisibility(View.GONE);
holder.nameView.setTextSize(23);
//holder.constraintView.setBackgroundColor(getResources().getColor(R.color.office_color));
}
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("1")) {
holder.profileIcon.setTypeface(font);
holder.ageView.setTypeface(font);
holder.ageView.setText(" شركة");
holder.ageView.setTextSize(17);
holder.profileIcon.setText("\uf0f7");
holder.experinceView.setVisibility(View.GONE);
holder.nameView.setTextSize(23);
//holder.constraintView.setBackgroundColor(getResources().getColor(R.color.company_color));
}
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("3")){
holder.profileIcon.setVisibility(View.GONE);
holder.ageView.setTypeface(typeface);
}
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("3") && mCursor.getString(ArticleLoader.Query.STATUE).equals("1")){
holder.ageView.setText(mCursor.getString(ArticleLoader.Query.AGE) + " سنة" + " / " + "متاح ");
}else if(mCursor.getString(ArticleLoader.Query.TYPE).equals("3") && mCursor.getString(ArticleLoader.Query.STATUE).equals("0")){
holder.ageView.setText(mCursor.getString(ArticleLoader.Query.AGE) + " سنة" + " / " + "غير متاح ");
}else if (mCursor.getString(ArticleLoader.Query.TYPE).equals("3")){
holder.ageView.setText(mCursor.getString(ArticleLoader.Query.AGE) + " سنة");
}
// holder.dateView.setText(DateUtils.getRelativeTimeSpanString(
// mCursor.getLong(ArticleLoader.Query.PUBLISHED_DATE),
// System.currentTimeMillis(), DateUtils.HOUR_IN_MILLIS,
// DateUtils.FORMAT_ABBREV_ALL).toString() + "قبل");
holder.experinceView.setText(mCursor.getString(ArticleLoader.Query.EXPERINCE) + " سنوات خبرة" + " / " + mCursor.getString(ArticleLoader.Query.CITY));
if (mCursor.getString(ArticleLoader.Query.THUMB_URL).length() > 5) {
Picasso.with(getApplicationContext()).load(mCursor.getString(ArticleLoader.Query.THUMB_URL)).into(holder.thumbnailView);
} else {
Picasso.with(getApplicationContext()).load("http:logo.png").into(holder.thumbnailView);
}
holder.nameView.setTypeface(typeface);
holder.experinceView.setTypeface(typeface);
holder.technicalProfile.setTypeface(typeface);
}
@Override
public int getItemCount() {
if (mCursor.getCount() < 1){
Toast.makeText(DisplaysActivity.this, "لا يوجد محتوى", Toast.LENGTH_SHORT).show();
}
return mCursor.getCount();
}
}
这个工作正常,但我的滚动问题,如果我向下滚动并再次向上滚动已更改的项目效果, 这是一个错误还是什么?
答案 0 :(得分:1)
我认为问题在于您的观点是gone
。视图被循环使用,假设视图#12已设置为将图像设置为gone
。我们还要说#12被循环使用以显示与数据库不同的行,并且该行要求图像可见或不可见gone
。视图#12中的图片,因为它已被回收,但仍然是gone
,除非您明确指出它visible
。换句话说,在您更改之前,循环视图中的所有信息都保持不变。这适用于数据和任何视图属性。很容易记住更改数据而不是视图属性。
我建议你添加代码,以便在需要显示视图时明确地使视图可见。
尝试使用以下前两行的onBindViewHolder()
来查看是否有帮助:
holder.experinceView.setVisibility(View.VISIBLE);
holder.profileIcon.setVisibility(View.VISIBLE);
答案 1 :(得分:0)
根据我遇到的适配器问题,如果你在适配器中处理视图的条件可见性,你必须考虑下面的所有条件检查
if (mCursor.getString(ArticleLoader.Query.TYPE).equals("2")) {
holder.profileIcon.setTypeface(font);
holder.ageView.setTypeface(font);
holder.ageView.setText(" مكتب" );
holder.ageView.setTextSize(17);
holder.profileIcon.setText("\uf0f7");
holder.experinceView.setVisibility(View.GONE);
holder.nameView.setTextSize(23);
//holder.constraintView.setBackgroundColor(getResources().getColor(R.color.office_color));
}
else if (mCursor.getString(ArticleLoader.Query.TYPE).equals("1")) {
holder.profileIcon.setTypeface(font);
holder.ageView.setTypeface(font);
holder.ageView.setText(" شركة");
holder.ageView.setTextSize(17);
holder.profileIcon.setText("\uf0f7");
holder.experinceView.setVisibility(View.GONE);
holder.nameView.setTextSize(23);
//holder.constraintView.setBackgroundColor(getResources().getColor(R.color.company_color));
}
else if (mCursor.getString(ArticleLoader.Query.TYPE).equals("3")){
holder.profileIcon.setVisibility(View.GONE);
holder.ageView.setTypeface(typeface);
///here you have to consider other views used in the conditions above to default behaviour you want
}
else{
///here you have to handle default behaviour
}
将视图设置为对特定条件不可见后,您需要处理可见性以查看其他内容,否则会导致滚动或数据更改出现问题