作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 recyclerview 中将重力设置为正确,我为所有布局设置了正确的重力,但是当水平回收 View 只有一行时,它没有将重力设置为正确看图了解我picture
我希望图片中的红色圆圈设置重力向右。
我的 CityDetailACT Activity :
package safarkon.com.safarkon.cityDetail;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import java.util.ArrayList;
import safarkon.com.safarkon.R;
public class CityDetailACT extends AppCompatActivity {
private RecyclerViewDataAdapter rAdapterBazar;
ArrayList<SectionDataModelRecyclerModel> allSampleData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.city_detail_act);
allSampleData = new ArrayList<SectionDataModelRecyclerModel>();
rvListBazar.setHasFixedSize(true);
rvListBazar.setNestedScrollingEnabled(false);
rAdapterBazar = new RecyclerViewDataAdapter(CityDetailACT.this, allSampleData);
rvListBazar.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
rvListBazar.setAdapter(rAdapterBazar);
createDummyData();
}
public void createDummyData() {
for (int i = 2; i <= 8; i++) {
SectionDataModelRecyclerModel dm = new SectionDataModelRecyclerModel();
String cat = null;
if (i == 1){
//cat = "بدانید";
dm.setHeaderTitle("هر آنچه درمورد "+city+" باید بدانید");
}else if (i == 2){
cat = "بازار";
dm.setHeaderTitle("بازارهای "+city);
}else if (i == 3){
cat = "دیدنی";
dm.setHeaderTitle("معرفی مکانهای گردشگری "+city);
}else if (i == 4){
cat = "تاریخی";
dm.setHeaderTitle("دیدنی های تاریخی "+city);
}else if (i == 5){
cat = "پارک";
dm.setHeaderTitle("پارک های "+city);
}else if (i == 6){
cat = "مسجد";
dm.setHeaderTitle("مسجدها و امام زاده های "+city);
}else if (i == 7){
cat = "موزه";
dm.setHeaderTitle("موزه های "+city);
}else if (i == 8){
cat = "مدرسه";
dm.setHeaderTitle("مدرسه های "+city);
}
ArrayList<RecyclerModel> singleItem = new ArrayList<RecyclerModel>();
LoadData.firstLoadData(this,rAdapterBazar,singleItem,rvListBazar);
dm.setAllItemsInSection(singleItem);
allSampleData.add(dm);
}
}
}
RecyclerViewDataAdapter:
package safarkon.com.safarkon.cityDetail;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import safarkon.com.safarkon.R;
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {
private ArrayList<SectionDataModelRecyclerModel> dataList;
private Context mContext;
public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModelRecyclerModel> dataList) {
this.dataList = dataList;
this.mContext = context;
}
@Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_row_nested_list, null);
ItemRowHolder mh = new ItemRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {
final String sectionName = dataList.get(i).getHeaderTitle();
ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();
itemRowHolder.itemTitle.setText(sectionName);
RecyclerAdapter itemListDataAdapter = new RecyclerAdapter(singleSectionItems,"a",mContext);
itemRowHolder.recycler_view_list.setHasFixedSize(true);
itemRowHolder.recycler_view_list.setItemViewCacheSize(20);
itemRowHolder.recycler_view_list.setDrawingCacheEnabled(true);
itemRowHolder.recycler_view_list.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);
itemRowHolder.recycler_view_list.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, true));
}
@Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected TextView itemTitle;
protected RecyclerView recycler_view_list;
protected TextView txMore;
public ItemRowHolder(View view) {
super(view);
this.itemTitle = view.findViewById(R.id.itemTitle);
this.recycler_view_list =view.findViewById(R.id.recycler_view_list);
this.txMore= view.findViewById(R.id.txMore);
}
}
}
回收器适配器:
package safarkon.com.safarkon.cityDetail;
import android.content.Context;
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 com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import safarkon.com.safarkon.R;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<RecyclerModel> recyclerModels; // this data structure carries our title and description
Context c;
String rowLayoutType;
int width;
public RecyclerAdapter(ArrayList<RecyclerModel> recyclerModels,String rowLayoutType, Context c) {
this.recyclerModels = recyclerModels;
this.rowLayoutType = rowLayoutType;
this.c = c;
}
public RecyclerAdapter(ArrayList<RecyclerModel> recyclerModels,String rowLayoutType, Context c,int width) {
this.recyclerModels = recyclerModels;
this.rowLayoutType = rowLayoutType;
this.c = c;
this.width = width;
}
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_city_detail, parent, false));
}
@Override
public void onBindViewHolder(final RecyclerAdapter.MyViewHolder holder,final int position) {
Picasso.with(c)
.load(recyclerModels.get(position).getPicture())
.resize(width,0)
.into(holder.imageView);
holder.tx_onvan.setText(recyclerModels.get(position).getOnvan());
holder.tx_position.setText(recyclerModels.get(position).getPosition());
holder.ratingBar.setRating((float)recyclerModels.get(position).getRate());
holder.txCountRateAndComment.setText("( "+recyclerModels.get(position).getCountRateAndComment() + " نظر" + " )");
}
@Override
public int getItemCount() {
return recyclerModels.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView txCountRateAndComment;;
TextView tx_onvan;
TextView tx_position;
ImageView imageView;
SimpleRatingBar ratingBar;
MyViewHolder(View view) {
super(view);
tx_onvan= itemView.findViewById(R.id.nameTxt);
tx_position= itemView.findViewById(R.id.txPostion);
imageView = itemView.findViewById(R.id.imageView2);
ratingBar= itemView.findViewById(R.id.simpleRatingBar);
txCountRateAndComment = itemView.findViewById(R.id.txCountRateAndComment);
}
}
}
city_detail_act:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top"
>
<android.support.v7.widget.Toolbar
android:id="@+id/tolbar_story"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#66cc66"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
android:orientation="horizontal"
tools:layout_editor_absoluteX="16dp">
<ImageView
android:id="@+id/imgTooTitlelbarMainActf"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/adamak" />
<ImageView
android:id="@+id/imgTooTitlelbarMainAct"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="3dp"
android:layout_marginEnd="181dp"
android:layout_marginRight="181dp"
android:layout_marginBottom="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/title" />
<ImageView
android:id="@+id/imgNavigationView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="false"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/dot" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"
android:layout_marginBottom="12dp"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="@+id/txMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/adobearabicbold"
android:text="مشاهده همه..."
android:textSize="12sp"
/>
<TextView
android:id="@+id/txYHinCityDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:fontFamily="@font/adobearabicbold"
android:text="هر آنچه درمورد قم باید بدانید"
android:textSize="24sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvListBazar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
当水平回收 View 只有一行时,我想将重力设置为右边,因为当水平 recyclerview 有更多一行时,它的重力设置为正确,我该怎么做?
最佳答案
在您的 RecyclerViewDataAdapter 中,将 setLayotManager
的最后一个参数设置为 false,如果这不起作用,请将 LinearLayoutManager.setStatckFromEnd 属性设置为 true
关于android - 我如何为行 recyclerview 设置重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58572453/
我是一名优秀的程序员,十分优秀!