我正在使用UlimateRecyclerView,它扩展了RecyclerView的许多功能。它应该像GitHub上的普通RecyclerView一样使用。
当运行代码时,元素之间有一个很大的空间,我不知道为什么......
我希望你能帮忙!
问题的截图 UltimateRecyclerView Screenshot
我的XML with ultimateRecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sherzad.recyclerviewuebung.MainActivity">
<com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ultimate_recyclerView"/>
</RelativeLayout>
MainActivity
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
public class MainActivity extends AppCompatActivity {
UltimateRecyclerView dhikrListe;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dhikrListe = (UltimateRecyclerView) findViewById(R.id.ultimate_recyclerView);
dhikrListe.setHasFixedSize(true);
dhikrListe.setLayoutManager(new LinearLayoutManager(this)); //Hier könnten wir auch verschiedene Layouts festlegen. Bei der ListView, die vorher hatten, ist das Linearlayout fest eingestellt.
dhikrListe.addItemDecoration(
new HorizontalDividerItemDecoration.Builder(this)
.color(Color.GRAY)
.build()); // Der Divider trennt die Zeilen mit einem Strich
myAdapter = new MyAdapter(this);
dhikrListe.setAdapter(myAdapter); //Adapter wird gesetzt
}
}
我的模型/对象
public class Dhikr {
private int id;
private String text = "";
public int getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Dhikr(String text) {
this.text = text;
}
}
我的适配器
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.marshalchen.ultimaterecyclerview.UltimateDifferentViewTypeAdapter;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sherzad on 20.11.16.
*/
public class MyAdapter extends UltimateViewAdapter{
List<Dhikr> dhikrListe;
private MainActivity activity;
public MyAdapter(MainActivity mainActivity){
this.activity = mainActivity;
dhikrListe = new ArrayList<Dhikr>();
dhikrListe.add(new Dhikr("Hello!"));
dhikrListe.add(new Dhikr("yess!"));
}
public static class DhikrViewHolder extends UltimateRecyclerviewViewHolder{
public TextView dhikrText;
public DhikrViewHolder(View view) {
super(view);
dhikrText = (TextView) view.findViewById(R.id.textView);
}
}
@Override //Hier wird das Layout vorbereitet und geladen
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = this.activity.getLayoutInflater().inflate(R.layout.dhikr, parent, false);
return new DhikrViewHolder(view);
}
@Override
public RecyclerView.ViewHolder newFooterHolder(View view) {
return null;
}
@Override
public RecyclerView.ViewHolder newHeaderHolder(View view) {
return null;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
return null;
}
//Hier werden die Daten befüllt
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Dhikr dhikr = dhikrListe.get(position);
DhikrViewHolder dhikrHolder = (DhikrViewHolder) holder; //Wir wandeln den holder (der in dieser methode übergeben wird) in unseren eigenen TodoViewHolder um, um auf seine Eigenschaften (todoTitel) zugreifen zu können und im nächsten Schritt ihn zu setzen.
dhikrHolder.dhikrText.setText(dhikr.getText());
}
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
return null;
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return dhikrListe.size();
}
@Override
public int getAdapterItemCount() {
return 0;
}
@Override
public long generateHeaderId(int position) {
return 0;
}
}