2016-09-29 96 views
1

--- TLDR ---

我下面的教程,其中有cardview和recyclerview的幫助下,我應該能夠列出對方喜歡在這下面的文字: 應該如何樣的樣子(不需要圖像) 的Android CardView填充錯誤

它是如何看起來

--- ---問題

正如標題所述,並且可以在圖片上看到,它看起來像是一個巨大的2個cardview項目之間的填充,這顯然不是我想要的東西。我希望他們接近彼此,因爲它可以在其他圖片上看到。

請記住,即時超級初學者程序員:D。提前感謝所有答案。

--- CODE ---

cards_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:tag="cards main container"> 

     <android.support.v7.widget.CardView 
      android:layout_width="300dp" 
      android:layout_height="60dp" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      card_view:cardElevation="5dp" 
      card_view:cardUseCompatPadding="true" 
      card_view:cardBackgroundColor="#FFF" 
      android:id="@+id/card_view" 
      android:layout_gravity="center_horizontal"> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:layout_marginTop="10dp" 
         android:text="Marketing Tip" 
         android:id="@+id/textviewName" 
         android:textColor="#000"/> 

       </LinearLayout> 

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

</LinearLayout> 

HomeActivity.java

import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 

import java.util.ArrayList; 

public class HomeActivity extends AppCompatActivity { 

    private static RecyclerView.Adapter adapter; 
    private RecyclerView.LayoutManager layoutManager; 
    private static RecyclerView recyclerView; 
    private static ArrayList<DataModel> data; 
    static View.OnClickListener myOnClickListener; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     setTitle("Home"); 


     myOnClickListener = new myOnClickListener(this); 


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

     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 

     data = new ArrayList<DataModel>(); 
     for (int i = 0; i < MyData.nameArray.length; i++) { 
      data.add(new DataModel(
        MyData.nameArray[i], 
        MyData.id_[i] 
      )); 

      adapter = new CustomAdapter(data); 
      recyclerView.setAdapter(adapter); 

     } 

    } 

     private static class myOnClickListener implements View.OnClickListener { 

      private final Context context; 

      private myOnClickListener(Context context) { 
       this.context = context; 
      } 

      @Override 
      public void onClick(View v) { 
       //TODO WRITE THE CODE TO START AN ACTIVITY ONCE THE CARDVIEW IS CLICKED 
      } 
     } 

} 

CustomAdapter.java

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{ 

    private ArrayList<DataModel> dataSet; 

    public static class MyViewHolder extends RecyclerView.ViewHolder { 

     TextView textviewName; 


     public MyViewHolder(View itemView) { 
      super(itemView); 
      this.textviewName = (TextView)itemView.findViewById(R.id.textviewName); 

     } 
    } 

    public CustomAdapter(ArrayList<DataModel> data){ 
     this.dataSet = data; 

    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards_layout, parent, false); 
     view.setOnClickListener(HomeActivity.myOnClickListener); 

     MyViewHolder myViewHolder = new MyViewHolder(view); 

     return myViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) { 

     TextView textviewName = holder.textviewName; 

     textviewName.setText(dataSet.get(listPosition).getName()); 


    } 

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

DataModel.java

public class DataModel { 
    String name; 
    int id_; 

    public DataModel(String name, int id_) { 

     this.name = name; 
     this.id_ = id_; 
    } 

    public String getName() { 
     return name; 
    } 

    public int getId() { 
     return id_; 
    } 
} 

MyData.java

public class MyData { 

    static String[] nameArray = {"Buzi", "Kocsog", "Fostalicska", "CiganyTibi"}; 

    static Integer[] id_ = {0, 1, 2, 3}; 
} 
+0

爲什麼你硬編碼cardview寬** android:layout_width =「300dp」**? 將其更改爲** android:layout_width =「match_parent」** –

回答

1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:tag="cards main container"> 

     <android.support.v7.widget.CardView 
      android:layout_width="300dp" 
      android:layout_height="60dp" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      card_view:cardElevation="5dp" 
      card_view:cardUseCompatPadding="true" 
      card_view:cardBackgroundColor="#FFF" 
      android:id="@+id/card_view" 
      android:layout_gravity="center_horizontal"> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:layout_marginTop="10dp" 
         android:text="Marketing Tip" 
         android:id="@+id/textviewName" 
         android:textColor="#000"/> 

       </LinearLayout> 

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

</LinearLayout> 

使用此作爲你的cardview佈局。你基本上已經使用match_parent作爲線性佈局,使視圖填滿整個屏幕。將其更改爲wrap_content應該可以完成這項工作。休息很好。

1

Heyho,

我也是一個初學者,但我想它是由於你在cards_layout.xml中的linearlayout的高度。

嘗試使用wrap_content而不是match_parent。 否則,你會膨脹一張卡,與您recyclerView匹配的大小。

希望這可以幫助你。

托馬斯

1
<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/card_task" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    card_view:contentPadding="16dp" 
    card_view:cardElevation="2dp" 
    card_view:cardCornerRadius="5dp" 
    layout_height="wrap_content" 
    layout_width="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

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


     <TextView 
      android:id="@+id/desc" 
      android:padding="5dp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Description" /> 

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

以上佈局文件爲我工作。希望它有幫助