2016-07-27 6 views

Antwort

2

Die Bilder dass Sie in Ihrer Listenansicht sein wollen, dh die Elemente in der Listenansicht sind nichts anderes als ein Layout. Sie können ein eigenes Layout erstellen, in das Sie das Bild und die Schaltfläche einfügen können, wie Sie möchten.

Dazu müssen Sie einen eigenen Adapter implementieren, mit dem Sie jedes neue Element in der Listenansicht mit seinem eigenen definierten Bildpfad und dem Button-Text oder dem TextView-Text erstellen können.

Sie können separate Bilder und Text für jedes Element in der Listenansicht definieren, indem Sie Ihren eigenen Adapter verwenden.

Beispiel:

ArrayAdapter:

public class UsersAdapter extends ArrayAdapter<User> { 
    public UsersAdapter(Context context, ArrayList<User> users) { 
     super(context, 0, users); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     User user = getItem(position);  
     // Check if an existing view is being reused, otherwise inflate the view 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false); 
     } 
     // Lookup view for data population 
     TextView tvName = (TextView) convertView.findViewById(R.id.tvName); 
     TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome); 
     // Populate the data into the template view using the data object 
     tvName.setText(user.name); 
     tvHome.setText(user.hometown); 
     // Return the completed view to render on screen 
     return convertView; 
    } 
} 

Benutzerklasse:

public class User { 
    public String name; 
    public String hometown; 

    public User(String name, String hometown) { 
     this.name = name; 
     this.hometown = hometown; 
    } 
} 

Für weitere Informationen: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

2

Verwenden cardvi ew und recyclerview

erstellen cardAdapter

CardAdapter.class

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> { 
    private static Context context; 
    View cardview; 
    Cursor dataCursor; 
    public static class ViewHolder extends RecyclerView.ViewHolder { 
     public View view; 
     public TextView title; 
     public ImageView imageView; 
     public ViewHolder(View v) { 
      super(v); 
      view = v; 
      title = (TextView) v.findViewById(R.id.topic); 
      imageView = (ImageView) v.findViewById(R.id.image); 

      v.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Log.d("RECYCLER", "CLICK"); 

       } 
      }); 
     } 
    } 
    public CardAdapter(Context context, Cursor cursor) { 
     CardAdapter.context = context; 
     dataCursor = cursor; 
    } 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     cardview = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.cardview, parent, false); 
     return new ViewHolder(cardview); 
    } 

    public Cursor swapCursor(Cursor cursor) { 
     if (dataCursor == cursor) { 
      return null; 
     } 
     Cursor oldCursor = dataCursor; 
     this.dataCursor = cursor; 
     if (cursor != null) { 
      this.notifyDataSetChanged(); 
     } 
     return oldCursor; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     dataCursor.moveToPosition(position); 

     //handle database queries using the dataCursor 
    } 
    @Override 
    public int getItemCount() { 
     //replace 8 with 0 when you pass a cursor into the recycler in DemosFragment 
     return (dataCursor == null) ? 8 : dataCursor.getCount(); 
    } 
} 

Entwurf der Kartenlayout

cardview.xml

<?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:id="@+id/card_view" 
    android:layout_gravity="center" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_marginLeft="@dimen/card_margin" 
    android:layout_marginRight="@dimen/card_margin" 
    android:layout_marginTop="8dp" 
    android:layout_marginBottom="8dp" 
    android:clickable="true" 
    android:orientation="vertical" 
    android:foreground="?attr/selectableItemBackground"> 
    <LinearLayout android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_weight="2" 
      android:layout_height= "0dp"> 

      <ImageView 
       android:id="@+id/image" 
       android:scaleType="centerCrop" 
       android:src="@drawable/nav_image" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" 
       android:id="@+id/button" 
       android:layout_gravity="right|center_vertical" /> 

     </FrameLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:layout_marginBottom="@dimen/activity_vertical_margin" 
       android:layout_marginTop="@dimen/activity_vertical_margin" 
       android:layout_marginLeft="@dimen/card_text_margin" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/topic" 
        android:text="@string/topic"/> 
      </LinearLayout> 
    </LinearLayout> 

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

hinzufügen Fragment/Aktivität

DemosFragment

public class DemosFragment extends Fragment { 
    private RecyclerView recyclerView; 
    CardAdapter adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.Demos_layout, container, false); 
     recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_record); 
     recyclerView.setHasFixedSize(true); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
     adapter = new CardAdapter(getActivity(), null); 
     recyclerView.setAdapter(adapter); 
     return rootView; 
    } 
} 

Schließlich

compile 'com.android.support:design:23.1.1' 

compile 'com.android.support:cardview-v7:23.1.1' 

compile 'com.android.support:recyclerview-v7:23.1.1' 

Eine vollständige Probe meiner project mit dem Konzept der Einfuhren nicht vergessen

Verwandte Themen