recycleview两次显示相同的cardview数据

时间:2016-04-01 07:26:33

标签: android android-recyclerview android-cardview

the same cardview data called twice and overlapping

我是编程新手。我只需要你对recyclerview的指导。正如您在图像中看到的那样(请单击链接查看图像,因为我无法直接附加图像),相同的数据会被调用两次。我不知道是什么原因。请帮忙。

这是我的activity_main.xml     

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<fragment 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/fragment"
    android:name="com.georgekurniawan.radiorohaniindonesia.MainActivityFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />

my fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.georgekurniawan.radiorohaniindonesia.MainActivityFragment"
>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/rv"
        />

cardview

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="@dimen/cardview_default_elevation"
android:foreground="?android:attr/selectableItemBackground"
android:layout_margin="0dp"
android:id="@+id/cv_radio"
>

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/logo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:maxWidth="140dp"
            android:scaleType="center" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio_name"
            android:layout_toRightOf="@+id/logo"
            android:layout_alignParentTop="true"
            android:textSize="21sp"
            android:gravity="center_horizontal|center_vertical" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio_gelombang"
            android:layout_toRightOf="@+id/logo"
            android:layout_below="@+id/radio_name"
            android:gravity="center_horizontal|center_vertical" />

    </RelativeLayout>

我的recycleadapter

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {

public ArrayList<StasiunRadio> listradio;
RecycleAdapter(ArrayList<StasiunRadio> listradio){
    this.listradio = listradio;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    private CardView cv;
    private TextView mNama;
    private TextView mGelombang;
    private ImageView mLogo;

    public ViewHolder(View v) {
        super(v);
        cv=(CardView)v.findViewById(R.id.cv_radio);
        mNama=(TextView)v.findViewById(R.id.radio_name);
        mGelombang=(TextView)v.findViewById(R.id.radio_gelombang);
        mLogo=(ImageView)v.findViewById(R.id.logo);
    }
}

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

@Override
public RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cardview_radio, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int i) {
    StasiunRadio list = listradio.get(i);
    holder.mNama.setText(list.getNama());
    holder.mGelombang.setText(list.getGelombang());
    holder.mLogo.setImageResource(list.getLogo());
}}

main_activity_fragment

public class MainActivityFragment extends Fragment {
private RecycleAdapter rAdapter;
StasiunRadio radio = new StasiunRadio();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View myView = inflater.inflate(R.layout.fragment_main, container, false);
    RecyclerView rvList = (RecyclerView) myView.findViewById(R.id.rv);

    rvList.setHasFixedSize(true);
    RecyclerView.LayoutManager layout;
    layout = new LinearLayoutManager(getActivity());
    rvList.setLayoutManager(layout);
    ArrayList<StasiunRadio> radiolist;
    radio.inputradio();
    radiolist = radio.getALL();
    rAdapter = new RecycleAdapter(radiolist);
    rvList.setAdapter(rAdapter);
    rvList.setItemAnimator(new DefaultItemAnimator());
    return myView;
}}

MainActivity java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment, new MainActivityFragment())
                .commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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);
}}

1 个答案:

答案 0 :(得分:0)

删除此

 if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment, new MainActivityFragment())
                .commit();
    }

因为您在xml中添加了片段,所以此代码正在片段中添加片段,该片段已经存在。