2017-06-04 8 views
1

私はこのような何かをするカードレイアウトでリサイクルビューを実装しています: imageカードレイアウトとリサイクルビュー常に同じサイズ

事は、私は完全な幅/高さのカードのレイアウトを得ることができないということであり、

image2

どのように私は私が望む結果を得るために、私の例を適応させることができます、私はちょうどこのように低いカードレイアウトを取得しておきますか?

私はすでに、カードビューの内側にそれを貼り付けるためのすべての情報を持つアダプタを持っていますが、どういうわけか、レイアウトは常に奇妙です。ここで

は私mainLayoutの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> 

アクティビティコード

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) { 

    } 
} 

任意のヘルプ、してください?

は、あなたがあなたのCardViewが総screen-width1/5スペースを使用しています理由ですスパン数5であなたのRecyclerViewためGridLayoutManagerを使用している

+0

は、あなたのアクティビティコードを投稿役立つことを願っています。 – FAT

+0

doneこれを今すぐチェックしてください –

答えて

0

1.ありがとうございます。使用LinearLayoutManagerfull-width垂直リストとして表示する

recyclerView.setLayoutManager(new LinearLayoutManager(this)); 

の代わりに:

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 5); 
recyclerView.setLayoutManager(layoutManager); 

2.あなたはwrap_contentとしてRecyclerView幅と高さを使用しています。 match_parentを試してみてください。

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

これは〜

+0

私はカスタムツールバーを持っているので、リサイクルビューを少し下に移動することはできません。 –

+0

'DrawerLayout'は最大2つの直接の子を持つことができるので、' AppBarLayout'の下に 'DrawerLayout'から' RecyclerView'を取り除き、 'AppBarLayout'の中に' app_bar_plant_feed'を入れてください。また、RecyclerView xmlに 'app:layout_behavior =" @ string/appbar_scrolling_view_behavior "'を追加してToolBarの下に表示してください。 – FAT

+0

私はapp_bar_plant_feedを作成する必要がありますか? –

関連する問題