Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 卡片布局始终相同大小的循环视图_Java_Android_Xml_Android Layout_Android Design Library - Fatal编程技术网

Java 卡片布局始终相同大小的循环视图

Java 卡片布局始终相同大小的循环视图,java,android,xml,android-layout,android-design-library,Java,Android,Xml,Android Layout,Android Design Library,我正在实现一个具有卡片布局的回收视图,以执行以下操作: 问题是,我无法获得全宽/全高的卡片布局,我只是不断获得像这样的低卡片布局: 我如何调整我的示例以获得我想要的结果 我已经有了一个包含所有信息的适配器,可以将它粘贴到卡片视图中,但不知何故,它的布局总是那么奇怪:S 这是我的主布局的XML: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmln

我正在实现一个具有卡片布局的回收视图,以执行以下操作:

问题是,我无法获得全宽/全高的卡片布局,我只是不断获得像这样的低卡片布局:

我如何调整我的示例以获得我想要的结果

我已经有了一个包含所有信息的适配器,可以将它粘贴到卡片视图中,但不知何故,它的布局总是那么奇怪:S

这是我的主布局的XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_plant_feed"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_plant_feed"
            app:menu="@menu/activity_plant_feed_drawer"
            android:background="@color/white"/>

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

    </android.support.v4.widget.DrawerLayout>


**and the row for the card layout:**

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_specie_radius">

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


        <ImageView
                android:id="@+id/userIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_toEndOf="@+id/reportImg"
                android:src="@drawable/ic_user" />

            <TextView
                android:id="@+id/Avaliation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/plantName"
                android:layout_marginStart="92dp"
                android:layout_marginTop="15dp"
                android:layout_toEndOf="@+id/dateTxt"
                android:text="Avalie a fotografia" />

            <TextView
                android:id="@+id/dateTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="29dp"
                android:text="TextView" />

            <ImageView
                android:id="@+id/reportImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/color_cursor_white" />

            <ImageView
                android:id="@+id/plantPhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp" />

            <TextView
                android:id="@+id/plantName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/plantPhoto"
                android:textColor="@color/nephritis"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginStart="11dp"
                android:layout_toEndOf="@+id/userIcon" />

        </RelativeLayout>

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

    </LinearLayout>
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 5);
recyclerView.setLayoutManager(layoutManager);

**以及卡片布局的行:**
活动代码

public class PlantFeed extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener,PlantFeedAdapter.OnItemClickListener {

    //initialize fields

    String token;

    ArrayList<PlantPhotoUser> photos = new ArrayList<>();

    VolleyService mVolleyService;
    IResult mResultCallback = null;
    final String GETREQUEST = "GETCALL";

    final String URL = "http://10.0.2.2:3000/fotos";

    String date;
    String lat;
    String lon;
    String alt;

    PlantFeedAdapter plantFeedAdapter;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plant_feed);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        recyclerView = (RecyclerView)findViewById(R.id.recycleView);
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),5);
        recyclerView.setLayoutManager(layoutManager);
        plantFeedAdapter = new PlantFeedAdapter(getApplicationContext(), photos,PlantFeed.this);
        recyclerView.setAdapter(plantFeedAdapter);

        token = checkForToken();

        initVolleyCallback();

        mVolleyService = new VolleyService(mResultCallback,this);

        mVolleyService.getDataVolley(GETREQUEST,URL,token);

    }

    void initVolleyCallback(){
        mResultCallback = new IResult() {
            @Override
            public void notifySuccess(String requestType,JSONObject response) {
                Log.d("HELLL","hi1");
            }

            @Override
            public void notifySuccess(String requestType, JSONArray response) {
                PlantPhotoUser plantPhotoUser;
                Log.d("HELLLL","hi");
                for (int i=0; i < response.length(); i++) {
                    try {
                        JSONObject object = response.getJSONObject(i);
                        Log.d("objeto",object.toString());

                        int userId = object.getInt("userId");
                        Log.d("objeto",String.valueOf(userId));
                        String username = object.getJSONObject("user").getString("username");
                        Log.d("objeto",String.valueOf(username));
                        int plantId = object.getInt("plantId");
                        Log.d("objeto",String.valueOf(plantId));
                        String specie = object.getJSONObject("plant").getString("specie");
                        Log.d("objeto",String.valueOf(specie));

                        String path = object.getString("image");
                        Log.d("objeto",String.valueOf(path));
                        int fotoId = object.getInt("id");

                        if(object.getString("date") != null){
                            date = object.getString("date");
                        }

                        if(object.getString("lat") != null){
                            lat = object.getString("lat");
                        }

                        if(object.getString("lon") != null){
                            lon = object.getString("lon");
                        }

                        if(object.getString("altitude") != null){
                            alt = object.getString("altitude");
                        }

                        plantPhotoUser = new PlantPhotoUser(fotoId,plantId,userId,path,specie,date,lat,lon,alt,username);
                        photos.add(plantPhotoUser);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                plantFeedAdapter.notifyDataSetChanged();

            }

            @Override
            public void notifyError(String requestType,VolleyError error) {
                Log.d("FAIL",error.toString());
            }
        };
    }

    public String checkForToken() {
        SharedPreferences sharedPref = getSharedPreferences("user", MODE_PRIVATE);
        String tokenKey = getResources().getString(R.string.token);
        String token = sharedPref.getString(getString(R.string.token), tokenKey); // take the token
        return token;
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onRowClick(int position, String name, int id, View view) {

    }
}
公共类PlantFeed扩展了AppCompative活动
实现NavigationView.OnNavigationItemSelectedListener、PlantFeedAdapter.OnItemClickListener{
//初始化字段
字符串标记;
ArrayList photos=新建ArrayList();
截击发球;
IResult mResultCallback=null;
最后一个字符串GETREQUEST=“GETCALL”;
最终字符串URL=”http://10.0.2.2:3000/fotos";
字符串日期;
弦拉特;
弦长;
字符串alt;
PlantFeedAdapter PlantFeedAdapter;
回收视图回收视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u plant\u feed);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Snackbar.make(查看“替换为您自己的操作”,Snackbar.LENGTH\u LONG)
.setAction(“Action”,null).show();
}
});
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
recycleView=(recycleView)findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
RecycleView.LayoutManager LayoutManager=新的GridLayoutManager(getApplicationContext(),5);
recyclerView.setLayoutManager(layoutManager);
plantFeedAdapter=新的plantFeedAdapter(getApplicationContext(),照片,PlantFeed.this);
recyclerView.setAdapter(plantFeedAdapter);
令牌=checkForToken();
initVolleyCallback();
mVolleyService=新的截击服务(mResultCallback,this);
getDataVolley(GETREQUEST、URL、令牌);
}
void initVolleyCallback(){
mResultCallback=new-IResult(){
@凌驾
public void notifySuccess(字符串请求类型,JSONObject响应){
Log.d(“HELLL”、“hi1”);
}
@凌驾
public void notifySuccess(字符串请求类型,JSONArray响应){
PlantPhotoUser-PlantPhotoUser;
日志d(“helll”、“hi”);
对于(int i=0;i<android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />