2016-05-15 34 views
0

Ich möchte eine App, die Liste (RecyclerView) zeigt. Ich möchte ein einzelnes TextView in den Viewholder dieses RecyclerView einfügen.Einzelne Textansicht Liste in Recyclerview

MainActivity Code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 

    private CustomAdapter customAdapter; 
    ListView listView; 
    public Cursor cursor; 
    public StudentRepo studentRepo; 
    private final static String TAG = MainActivity.class.getName().toString(); 

    private static final String TAG_BOOKMARKS="bookmarks"; 
    private static final String TAG_ABOUT_US="about"; 

    //recyclerView implementation 
    private List<TopSample> wordlist=new ArrayList<>(); 
    private RecyclerView recyclerView; 
    private StudentAdapter studentAdapter; 
    //recyclerView implementation done 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     studentRepo = new StudentRepo(this); 

     //recyclerView implement 
     recyclerView=(RecyclerView)findViewById(R.id.recycler_view); 
     recyclerView.setHasFixedSize(true); 

     DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this); 
     databaseAccess.open(); 
     Cursor cursor= databaseAccess.getInfo(); 
     cursor.moveToFirst(); 
     do{ 
      TopSample topSample=new TopSample(cursor.getString(0)); 
      wordlist.add(topSample); 
     }while (cursor.moveToNext()); 
     databaseAccess.close(); 

     studentAdapter=new StudentAdapter(wordlist); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView.setLayoutManager(mLayoutManager); 
     //recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(studentAdapter); 

StudentAdapter Klasse

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

    private List<TopSample> wordslist= Collections.emptyList(); 
    public class MyViewHolder extends RecyclerView.ViewHolder{ 
     public TextView sword; 
     public MyViewHolder(View view){ 
      super(view); 
      sword=(TextView)view.findViewById(R.id.vocab); 
     } 
    } 

    public StudentAdapter(List<TopSample> wordslist){ 
     this.wordslist=wordslist; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.dictionary_list_row,parent,false); 
     MyViewHolder vh=new MyViewHolder(itemView); 
     return vh; 

     //return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     TopSample sample=wordslist.get(position); 
     holder.sword.setText(sample.getVocab()); 
    } 

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

TopSample Code:

public class TopSample { 

    public String vocab; 

    public TopSample(){} 

    public TopSample(String vocab){ 
     this.vocab=vocab; 
    } 

    public String getVocab() { 
     return vocab; 
    } 

    public void setVocab(String vocab) { 
     this.vocab = vocab; 
    } 
} 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_main" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" /> 

</RelativeLayout> 

dictionary_list_row.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:focusable="true" 
       android:clickable="true" 
       android:background="?android:attr/selectableItemBackground" 
       android:orientation="vertical"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:id="@+id/vocab" 
     android:textSize="32dp" 
     android:textStyle="bold"/> 

</RelativeLayout> 

So ist die Ausgabe wie wurde:

Current Output

Wie Sie, dass eine Person viewholder Größe längere Rechteck sehen nimmt wie erwartet und Recycler Ansicht ist voll match_parent nicht nehmen Befehl.

Ich wünsche eine Ausgabe wie diese hat, nur als Beispiel .: Expected Output just as an example

Ich habe versucht, auf veränderte Länge und Breite mit unterschiedlicher Kombination in den beide XML-Dateien, aber wie erwartet nicht das Ergebnis. Bitte schauen Sie und sagen Sie mir, wo ich falsch liege.

Nur noch eine Sache hinzuzufügen, ich extrahiere Daten aus externen Datenbanken, in denen ich erfolgreich bin, aber dieses Problem ist wahrscheinlich Layout-Problem, denke ich. Bitte korrigieren Sie mich.

+0

Ja, das Layoutproblem – Haroon

Antwort

0

in dictionary_list_row.xml

In Relative Layout

android:layout_height="wrap_content" 

Und von Textview

android:layout_height="wrap_content" 

Und

<android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" /> 
0

Problem ist, dass Sie TextView eine feste Höhe gegeben haben und Breite gegeben haben und Schriftgröße sehr groß zugewiesen haben .. das ist 32dp.

Verwenden Sie den folgenden Code. Sie können android:textSize="18sp" für die Standardgröße entfernen.

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/vocab" 
    android:textSize="18sp" 
    android:textStyle="bold" 
    /> 
0

Haben Sie versucht, die RelativeLayout Höhe zu setzen, anstatt match_parent wrap_content ??

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="true" 
      android:clickable="true" 
      android:background="?android:attr/selectableItemBackground" 
      android:orientation="vertical"> 
    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:id="@+id/vocab" 
    android:textSize="32dp" 
    android:textStyle="bold" 
    /> 
    </RelativeLayout> 
Verwandte Themen