这是我的情况我有列表视图(RecyclerView)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordiatelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_gray">
<TextView
android:id="@+id/reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/reload"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/questionlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/FloatingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:clickable="true"
app:layout_anchor="@id/questionlist"
app:layout_anchorGravity="right|bottom|end"
android:src="@drawable/ic_add_circle_outline_24dp"
/>
</android.support.design.widget.CoordinatorLayout>
我使用适配器维护此循环视图
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
private Context _Context;
private List<QuestionItem> Questions = new ArrayList<>();
private API api;
private int num;
private int lastPosition = -1;
public static final String BASE_POST_URl = "http://la3nyk.com/upload/";
public ListViewAdapter(Context context, List<QuestionItem> Questions) {
this._Context = context;
this.Questions = Questions;
api = new API(context);
}
public ListViewAdapter(Context context, List<QuestionItem> Questions,
int num) {
this._Context = context;
this.Questions = Questions;
this.num = num;
api = new API(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(_Context);
View itemView = inflater.inflate(R.layout.questions, null);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Typeface tf = Typeface.createFromAsset(_Context.getAssets(),
"fonts/GE_SS_Two_Bold.otf");
Typeface tfreg = Typeface.createFromAsset(_Context.getAssets(),
"fonts/GE_SS_Two_Light.otf");
ImageLoader _ImageLoader = MySingleton.getInstance(_Context).getmImageLoader();
holder.imageView.setImageUrl( BASE_POST_URl + Questions.get(position).getQue_image_url(), _ImageLoader);
holder.username.setTypeface(tf);
holder.username.setText(Questions.get(position).user.username);
holder.desc.setText(Questions.get(position).Que_Content);
holder.desc.setTypeface(tfreg);
holder.Date.setTypeface(tfreg);
holder.Category.setText(Questions.get(position).category);
holder.AnswersCount.setTypeface(tfreg);
holder.Date.setText(Questions.get(position).Que_Date);
holder.AnswersCount.setText(Questions.get(position).answers.size() + " " + _Context.getResources().getString(R.string.answers));
holder.AnswersCount.setVisibility(View.VISIBLE);
// if (num == 1)
// Questions.get(pos).user.userimageURL = api
// .getSetting(AppConstants.TAG_user_photo);
//
//
//
// if (Questions.get(pos).Que_image_url != null) {
// if (!Questions.get(pos).Que_image_url.contentEquals("")) {
// post_img.setVisibility(View.VISIBLE);
// //loader.DisplayImage(Questions.get(pos).Que_image_url, post_img,
// // post_img_pro, -1);
// }
// }
//
//
//if (num == 1) {
//arrow.setImageResource(R.drawable.p_data_7);}
//arrow.setVisibility(View.VISIBLE);
// Animation animation = null;
// animation = AnimationUtils.loadAnimation(_Context,
// R.anim.up_from_bottom);
// animation.setDuration(500);
// view.setAnimation(animation);
// animation = null;
//
// Animation animation = AnimationUtils.loadAnimation(_Context,
// (pos > lastPosition) ? R.anim.up_from_bottom
// : R.anim.down_from_top);
// view.startAnimation(animation);
// lastPosition = pos;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return Questions.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected NetworkImageView imageView , post_img;
protected TextView desc , Category ,username ,Date ,AnswersCount;
protected ProgressBar userimageprog ,post_img_pro;
protected ImageView arrow;
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.ques_user_img);
desc = (TextView) itemView.findViewById(R.id.ques_content);
Category = (TextView) itemView.findViewById(R.id.category);
post_img = (NetworkImageView) itemView.findViewById(R.id.post_img);
userimageprog = (ProgressBar) itemView.findViewById(R.id.userimgae_prog);
post_img_pro = (ProgressBar) itemView.findViewById(R.id.post_img_pro);
username = (TextView) itemView.findViewById(R.id.username_ques);
Date = (TextView) itemView.findViewById(R.id.date);
AnswersCount = (TextView) itemView.findViewById(R.id.answers_con);
arrow = (ImageView) itemView.findViewById(R.id.imageView1);
}
}
}
CardView xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardBackgroundColor="@android:color/white"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
cardview:cardCornerRadius="5dp"
cardview:cardElevation="2dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/category"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/blue"
android:padding="10dp"
android:text="TextView"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="10dp" >
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/ques_user_img"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/profile" />
<ProgressBar
android:id="@+id/userimgae_prog"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/username_ques"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Asmaa Samir"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:textColor="@android:color/black"
android:text="2 days ago" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/ques_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:text="ماهى اسباب ضعف الرؤيه أمام الحاسوب؟"
android:textColor="@android:color/black"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/qustion_8"
android:visibility="gone" />
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/post_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="right"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/answers_con"
android:visibility="gone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="32 إجابه" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
这是我的截图 我的问题是什么?
1-我获得了扭曲内容cardview,如图所示。
2-我的浮动按钮在视图开始加载时出现。
答案 0 :(得分:1)
在Adapter的createViewHolder方法中 改变这个
View itemView = inflater.inflate(R.layout.questions, null);
到
View itemView = inflater.inflate(R.layout.questions, parent,false);
我们需要将此卡附加到父级Recyclerview以继承其属性