即使我将布局宽度设置为matchparent,RecyclerView也仅显示一半屏幕

时间:2018-08-02 14:22:32

标签: android android-recyclerview

你好,我正在开发一个从服务器获取数据的应用,代码如下所示 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="match_parent"
 tools:context=".ImageQuiz">
 <android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar1"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="8dp"
    app:title="QUIZ"
    app:titleTextColor="#fff" />
 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e2e2e2">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="8dp"
            app:title="QUIZ"
            app:titleTextColor="#fff"
           />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <TextView
                    android:id="@+id/textView13"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Answer these questions"
                    android:textSize="10dp"
                    android:textAlignment="center"
                    android:padding="5dp"
                    android:textColor="#fff"
                    android:background="#05af43"
                    />

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/recylcerViewImage"/>


            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="Submit"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"
        android:textColor="#585858"
        android:background="@drawable/rounded_corner"

        android:layout_alignParentBottom="true"
        />


</RelativeLayout>

</android.support.constraint.ConstraintLayout>

ImageQuizAdapter.java

 package com.accolade.eventify;
    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.RadioButton;
import com.bumptech.glide.Glide;
import java.util.List;
public class ImageQuizAdapter extends RecyclerView.Adapter<ImageQuizAdapter.ImageQuizViewHolder> {
private Context mCtx;
    private List<ImageQuizModel> quizList;

    public ImageQuizAdapter (Context mCtx, List<ImageQuizModel> quizList) {
        this.mCtx = mCtx;
        this.quizList = quizList;
    }

@Override
public ImageQuizViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.layout_recycler_image_quiz, null);
    return new ImageQuizViewHolder(view);
}

@Override
public void onBindViewHolder(ImageQuizViewHolder holder, int position) {
    ImageQuizModel imageQuizModel=quizList.get(position);
    Glide.with(mCtx)
            .load(imageQuizModel.getImage())
            .into(holder.imageView);
        //here i used only image 


}


@Override
public int getItemCount() {
    return quizList.size();
}


public class ImageQuizViewHolder extends RecyclerView.ViewHolder {
    RadioButton r1,r2,r3,r4;
    ImageView imageView;
    public ImageQuizViewHolder(View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.imageView2);
        r1 = itemView.findViewById(R.id.radio_button1);


         r2 = itemView.findViewById(R.id.radio_button2);
            r3 = itemView.findViewById(R.id.radio_button3);
            r4 = itemView.findViewById(R.id.radio_button4);

        }
    }
}

ImagaQuiz.java

    package com.accolade.eventify;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ImageQuiz extends AppCompatActivity {
    private static final String URL_PRODUCTS = "http://accoladetest.cf/MyApi/MyApiQuizPic.php";

    RecyclerView recyclerView;
    private Toolbar mTopToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_quiz);
        mTopToolbar = (Toolbar) findViewById(R.id.my_toolbar1);
        setSupportActionBar(mTopToolbar);
        recyclerView = findViewById(R.id.recylcerViewImage);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        loadProducts();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_quiz, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_favorite) {
            Toast.makeText(this, "Add Feature", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void loadProducts() {

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            List<ImageQuizModel> data=new ArrayList<>();
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object
                            for(int i=0;i< array .length();i++){
                                JSONObject json_data =  array .getJSONObject(i);
                                ImageQuizModel product = new ImageQuizModel();
                                product.id= json_data.getInt("id");
                                product.image= json_data.getString("image");
                                product.op1= json_data.getString("op1");
                                product.op2= json_data.getString("op2");
                                product.op3= json_data.getString("op3");
                                product.op4= json_data.getString("op4");

                                data.add(product);
                                }

                            //creating adapter object and setting it to recyclerview
                            ImageQuizAdapter adapter = new ImageQuizAdapter(ImageQuiz.this, data);
                            recyclerView.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(this).add(stringRequest);
    }


 }

ImageQuizModel.java

 package com.accolade.eventify;

public class ImageQuizModel  {
    public int id;
    public String image;
    public String op1;
    public String op2;
    public String op3;
    public String op4;
    public ImageQuizModel(){

    }

    public String getImage() {
        return image;
    }
    public int getId(){
        return id;
    }

    public String getOp1() {
        return op1;
    }

    public String getOp2() {
        return op2;
    }

    public String getOp3() {
        return op3;
    }

    public String getOp4() {
        return op4;
    }
}

layout_recycler_image_guiz.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    app:cardCornerRadius="10dp"
    android:elevation="1dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/img" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio_button1"
                    android:text=" Option 1"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimary"
                    />
                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio_button2"
                    android:text="Option 2"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimary"/>
                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio_button3"
                    android:text="Option 3"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimary"/>
                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio_button4"
                    android:text="Option 4"
                    android:textSize="20dp"
                    android:buttonTint="@color/colorPrimary"/>

            </LinearLayout>
        </RadioGroup>
    </LinearLayout>
</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

但是这里的问题是,回收站视图显示在半屏中,如下图所示,我希望其宽度与父级匹配

enter image description here

3 个答案:

答案 0 :(得分:0)

转到您正在使用的imageview并确保将属性layout height和width都设置为match_parent。希望这会有所帮助。

答案 1 :(得分:0)

对于match_parent中的小部件,请勿使用ConstraintLayout。来自documentation

  

重要提示:不建议对ConstraintLayout中包含的窗口小部件使用MATCH_PARENT。可以通过使用MATCH_CONSTRAINT来定义类似的行为,并将相应的左/右或上/下约束设置为“父”。

如果您希望小部件在其父级上跨越100%,则在ConstraintLayout中执行此操作的方法是将小部件的宽度设置为0dp并指定开始和结束约束,如下所示RelativeLayout的子项ConstraintLayout

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#e2e2e2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

简而言之,ConstraintLayout的所有子代都应垂直和水平约束,match_parent不应使用。

答案 2 :(得分:0)

我删除了ConstraintLayout,取而代之的是我使用RelativeLayout作为父对象,它正在运行,谢谢大家的响应。

// MARK: Navigation Bar Customisation

// To change background colour.
UINavigationBar.appearance().barTintColor = .init(red: 23.0/255, green: 197.0/255, blue: 157.0/255, alpha: 1.0)

// To change colour of tappable items.
UINavigationBar.appearance().tintColor = .white

// To apply textAttributes to title i.e. colour, font etc.
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white,
                                                    .font : UIFont.init(name: "AvenirNext-DemiBold", size: 22.0)!]
// To control navigation bar's translucency.
UINavigationBar.appearance().isTranslucent = false
相关问题