2016-04-01 4 views
0

the same cardview data called twice and overlappingrecycleview отображает те же данные cardview дважды

Я новичок в программировании. Мне просто нужно ваше руководство по 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" /> 

мой 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 ява

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); 
}} 
+0

вставьте свой основной код операции, где вы добавляете фрагмент, кажется, что добавление дважды – Gaurav

+0

@GauravPolekar. Я уже прикрепляю код. Пожалуйста, посмотрите и помогите мне – onlygrace

ответ

0

удалить это

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

, потому что вы добавили фрагмент в XML, этот код добавляет фрагмент над фрагментом, который уже присутствует.

+0

спасибо, сейчас он работает красиво. Интересно, почему этот код нужно удалить, хотя этот код используется для присоединения фрагмента к mainactivity. Любое объяснение? – onlygrace

+0

проверить отредактированный ответ – Gaurav

Смежные вопросы