2014-02-02 2 views
1

Я хотел бы отображать элементы в моем ListView согласно изображению ниже.Android ListView - как отобразить

An entry in the listview

  • Квадрат слева изображение
  • Прямоугольники справа являются текстовые поля

Вопросы:

  • Что должен мой XML выглядит как?
  • Как вставить элементы там?

Текущий код:

Планировка:

activity_news_feed.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/newsfeed" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

list_single.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="6dip" 
     android:contentDescription="TODO" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/secondLine" 
     android:layout_width="fill_parent" 
     android:layout_height="26dip" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/icon" 
     android:ellipsize="marquee" 
     android:singleLine="true" 
     android:text="Description" 
     android:textSize="12sp" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/secondLine" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignWithParentIfMissing="true" 
     android:layout_toRightOf="@id/icon" 
     android:gravity="center_vertical" 
     android:text="Example application" 
     android:textSize="16sp" /> 

</RelativeLayout> 

активность:

package uk.ac.gla.serengeti.activities; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import uk.ac.gla.serengeti.R; 
import android.annotation.SuppressLint; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class NewsFeed extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_news_feed); 
      // Show the Up button in the action bar. 
      setupActionBar(); 

      final ListView listview = (ListView) findViewById(R.id.newsfeed); 

      String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", 
        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", 
        "Android", "iPhone", "WindowsMobile" }; 

      final ArrayList<String> list = new ArrayList<String>(); 
      for (int i = 0; i < values.length; ++i) { 
       list.add(values[i]); 
      } 
      final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list); 
      listview.setAdapter(adapter); 

      listview.setOnItemClickListener(new OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
        Toast.makeText(getApplicationContext(), 
         "Click ListItem Number " + position, Toast.LENGTH_LONG) 
         .show(); 
        } 
       });   
    } 

    private void setupActionBar() { 
      ActionBar actionBar = this.getSupportActionBar(); 
      actionBar.setDisplayHomeAsUpEnabled(true);    
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.news_feed, menu); 
      return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
      switch (item.getItemId()) { 
      case android.R.id.home: 
        // This ID represents the Home or Up button. In the case of this 
        // activity, the Up button is shown. Use NavUtils to allow users 
        // to navigate up one level in the application structure. For 
        // more details, see the Navigation pattern on Android Design: 
        // 
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
        // 
        //NavUtils.navigateUpFromSameTask(this); 
        finish(); 
        return true; 
      } 
      return super.onOptionsItemSelected(item); 
    } 

    private class StableArrayAdapter extends ArrayAdapter<String> { 

     HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); 

     public StableArrayAdapter(Context context, int textViewResourceId, 
      List<String> objects) { 
      super(context, textViewResourceId, objects); 
      for (int i = 0; i < objects.size(); ++i) { 
      mIdMap.put(objects.get(i), i); 
      } 
     } 

     @Override 
     public long getItemId(int position) { 
      String item = getItem(position); 
      return mIdMap.get(item); 
     } 

     @Override 
     public boolean hasStableIds() { 
      return true; 
     } 

     } 
} 

ответ

3
  1. Создать макет строки в XML-файле.

Он должен выглядеть примерно так. Вы, возможно, потребуется изменить веса макета:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="70dip" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLEft="true" 
     android:layout_marginRight="6dip" 
     android:contentDescription="TODO" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/secondLine" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/icon" 
     android:ellipsize="marquee" 
     android:singleLine="true" 
     android:text="Description" 
     android:textSize="12sp" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/secondLine" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignWithParentIfMissing="true" 
     android:layout_toRightOf="@id/icon" 
     android:gravity="center_vertical" 
     android:text="Example application" 
     android:textSize="16sp" /> 

</RelativeLayout> 
  1. Создать клиента адаптер вашего ListView, который простирается BaseAdapter

  2. переопределять метод GetView() из BaseAdapter; разверните макет и установите данные в представлениях в соответствии с положением.

    public View getView (int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from (контекст); convertView = inflater.inflate (R.layout.recipe_row, null);

    ImageView image = (ImageView) convertView.findViewById (R.id.image); 
        TextView title = (TextView) convertView.findViewById (R.id.title); 
        TextView description = (TextView) convertView.findViewById (R.id.description); 
    
        Recipe recipe = this.getItem (position); 
        image.setImageBitmap(recipte.getBitmap()); 
        title.setText(recipe.getTitle()); 
        description.setText(recipe.getDescription()); 
    
        return convertView; 
    } 
    
+0

Я борюсь с макетом :) – chuckfinley

+1

Конечно, пожалуйста, вставьте XML –

+0

Я обновил код. Я не уверен, когда и где следует раздувать макет list_single. – chuckfinley

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