android 浏览器查看项目未填充宽度

91zkwejq  于 2023-11-15  发布在  Android
关注(0)|答案(4)|浏览(147)

我设计了一个包含map fragment和configlerView的布局,每个configlerView项都是cardview(我指定了给予xml布局)。
问题是ClerView项目未填满屏幕宽度。img here
我尝试将layout_width更改为fill_parentmatch_parent.
这是每个项目的布局 *

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:id="@+id/cardView">

    <LinearLayout
        android:id="@+id/locationItemView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="120px"
            android:layout_height="120px"
            android:id="@+id/imgIcon"
            android:background="@drawable/image_bg"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <LinearLayout
            android:id="@+id/layoutInfo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Location Name"
                android:textColor="#d5c645"
                android:textStyle="bold"
                android:textSize="20dp"
                android:padding="3dp" />

            <TextView
                android:id="@+id/txtAddress"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Location Address"
                android:textSize="16dp"
                android:padding="3dp" />

            <TextView
                android:id="@+id/txtDistance"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="Location Distance"
                android:textSize="14dp"
                android:textStyle="italic"
                android:padding="2dp"
                android:textAlignment="viewEnd" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

字符串

main_layout

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:layout_height="0px" android:id="@+id/map" tools:context=".Main"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_weight=".6"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/locationList"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight=".4"
        android:divider="#FEFFCC"
        android:dividerHeight="1dp" />

</LinearLayout>

<ListView
    android:id="@+id/navdrawer"
    android:layout_width="250px"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="?attr/colorPrimaryDark"
    android:choiceMode="singleChoice"
    android:divider="@android:color/white"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="false"/>
</android.support.v4.widget.DrawerLayout>


希望有人能帮助我。我被它卡住了3天。谢谢。

public class LocationDetailsAdapter extends RecyclerView.Adapter<LocationDetailsViewHolder> {
    Context _context;
    ArrayList<LocationDetails> _data;

    public LocationDetailsAdapter(Context _context, ArrayList<LocationDetails> _object) {
        this._context = _context;
        _data = _object;
    }

    @Override
    public LocationDetailsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
        LocationDetailsViewHolder _viewHolder = new LocationDetailsViewHolder(_v);

        return _viewHolder;
    }

    @Override
    public void onBindViewHolder(LocationDetailsViewHolder locationDetailsViewHolder, int i) {
        LocationDetails _location = _data.get(i);
        locationDetailsViewHolder._imgType.setImageResource(R.drawable.repair_img);
        locationDetailsViewHolder._locationName.setText(_location.get_locationName());
        locationDetailsViewHolder._locationAddress.setText(_location.get_locationAddress() + ", " + _location.get_district() + ", " + _location.get_province());
        locationDetailsViewHolder._distance.setText(String.valueOf(_location.get_distance()) + " km");

        locationDetailsViewHolder._locationName.setOnClickListener(clickListener);
        locationDetailsViewHolder._imgType.setOnClickListener(clickListener);
        locationDetailsViewHolder._locationAddress.setOnClickListener(clickListener);

locationDetailsViewHolder._locationName.setTag(locationDetailsViewHolder);
        locationDetailsViewHolder._imgType.setTag(locationDetailsViewHolder);
        locationDetailsViewHolder._locationAddress.setTag(locationDetailsViewHolder);
}

    @Override
    public int getItemCount() {
        return (null != _data ? _data.size() : 0);
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LocationDetailsViewHolder _holder = (LocationDetailsViewHolder)v.getTag();
            int _pos = _holder.getPosition();

            int _id = _data.get(_pos).get_id();

            Intent _intent = new Intent(CommonFields._context, ItemView.class);
            _intent.putExtra("LocationID",_id);
            _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            CommonFields._context.startActivity(_intent);
        }
    };
}


这里我在main中得到RecylerView

_locationView = (RecyclerView)findViewById(R.id.locationList);
_locationView.setLayoutManager(new LinearLayoutManager(this));
_adapter = new LocationDetailsAdapter(Main.this, CommonFields._locationData);
_locationView.setAdapter(_adapter);

z4iuyo4d

z4iuyo4d1#

当从LayoutInflater膨胀View时,需要传递父参数以使用layout_*属性。这是因为这些属性需要创建正确的LayoutParams类。这意味着不能使用inflate(R.layout.*, null),而必须为第二个参数传递ViewGroup。在大多数情况下,你还想使用三参数的方法,并将false作为第三个参数传递。如果省略这一点或true,那么View将立即添加到父对象中,这会在像onCreateViewHolder()这样的地方导致问题,因为框架被设计成稍后执行此操作。有关更多细节,请参阅this answer
在你的情况下,

View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );

字符串
你应该改成

View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location, viewGroup, false );

f5emj3cl

f5emj3cl2#

您应该像这样创建View

@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {  
// create a new view
   View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.card_view, viewGroup, false);
// create ViewHolder
   ViewHolder viewHolder = new ViewHolder(itemLayoutView);
   return viewHolder;
 }

字符串

fzwojiic

fzwojiic3#

也许我的答案有不同的方法,但它的工作原理是占据屏幕的整个宽度,并且在通过回收视图时不会打破元素。

Adapter adapterBalanceInquiry = new Adapter(getActivity(), list);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapterBalanceInquiry);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                try {
                    int dx = (recyclerView.getWidth() - recyclerView.getChildAt(0).getWidth());
                    recyclerView.scrollBy(-dx, 0);
                    LinearSnapHelper snapHelper = new LinearSnapHelper();
                    recyclerView.setOnFlingListener(null);
                    snapHelper.attachToRecyclerView(recyclerView);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

字符串


的数据

h5qlskok

h5qlskok4#

改进的答案@RussHWolf如果你在适配器中使用数据绑定,

val binding = ViewBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )

return MyViewHolder(binding)

字符串
最后

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
        MyViewHolder{
        val binding = ViewBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return MyViewHolder(binding)
    }

相关问题