2016-07-06 62 views
0

我想創建一個ListView,其中包含一個ImageView和TextView在每一行。我能夠做到這一點,但我改變行之間的距離有問題。我想我改變了它,但我不知道如何。行之間的距離ListView Android

ActorAdapter.java:

ArrayList<Actors> actorList; 
LayoutInflater vi; 
int Resource; 
ViewHolder holder; 

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) { 
    super(context, resource, objects); 
    vi = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    Resource = resource; 
    actorList = objects; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // convert view = design 
    View v = convertView; 
    if (v == null) { 
     holder = new ViewHolder(); 
     v = vi.inflate(Resource, null); 
     holder.image= (ImageView) v.findViewById(R.id.miniimage); 
     holder.name = (TextView) v.findViewById(R.id.name); 
     holder.image.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("megnyomtam","a fost"); 
      } 
     }); 
     holder.name.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("megnyomtam","a fost"); 
      } 
     }); 
     v.setTag(holder); 
    } else { 
     holder = (ViewHolder) v.getTag(); 
    } 
    holder.image.setImageResource(R.drawable.ic_launcher); 
    new DownloadImageTask(holder.image).execute(actorList.get(position).getImageurl()); 
    holder.name.setText(actorList.get(position).getTitle()); 
    return v; 

} 

static class ViewHolder { 
    public ImageView image; 
    public TextView name; 

} 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 

} 

ActorFragment.java

ArrayList<Actors> actorsList; 
ActorAdapter adapter; 
ListView listview; 

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.activity_actor,parent, false); 
    actorsList = new ArrayList<Actors>(); 
    new JSONAsyncTask().execute("http://demo.breona.hu:8080/mobil/mobilService?method=getAllData&appid=ESK"); 

    listview = (ListView)v.findViewById(R.id.list25); 
    adapter = new ActorAdapter(getActivity().getApplicationContext(), R.layout.listview_row_layout, actorsList); 

    listview.setAdapter(adapter); 

    return v; 
} 

問題是在個XML也許隱藏,所以我複製了。

listview_row_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:focusableInTouchMode="true" 
android:background="@drawable/background"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/rellayout"> 

    <ImageView 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:id="@+id/miniimage" 
     android:src="@drawable/ic_launcher" 
     android:onClick="onClick"/> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/miniimage" 
     android:text="Cherry" 
     android:textColor="#000000" 
     android:onClick="onClick"/> 

</LinearLayout> 

activity_actor.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" android:background="#F1F1F1" 
tools:context=".MainActivity" > 

<ListView 
    android:id="@+id/list25" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:listitem="@layout/listview_row_layout"> 

</ListView> 

這裏是想什麼,我看到一張照片: https://www.bignerdranch.com/blog/customizing-android-listview-rows-subclassing/

+0

嘗試更改listview_row_layout.xml的高度或使用recyclerView並添加自定義分解器。 –

+0

你可以發佈你想要的圖片嗎 –

+0

如何?編程或在XML?我應該在listview_row_layout.xml中更改哪些內容? –

回答

1

爲了更改行之間的距離,您可以在包含id - rellayout的LinearLayout上使用填充/邊距。 (在你的listview_row_layout.xml)

,或者你可以創建,推動以上/該佈局下方空間視圖元素.. 像

//for example 10dp height 
<View android:layout_width="match_parent" 
     android:layout_height="10dp" 
     android:layout_below="@+id/rellayout"/> 

你可以給該視圖太多,如果你想等顏色..

個人我會得到主要佈局上的填充/邊界,而不是創建更多的視圖來繪製,但這是實現您的目標的另一種方式,我不知道你在做什麼,什麼是最好的方法給你。

+0

不幸的是無法工作。也許錯誤不在xml文件中? –

+0

不可能,你告訴我你添加了一個視圖,你沒有看到空間? – JozeRi

0
<ListView 
android:id="@+id/list" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:divider="@color/yourColor" 
android:dividerHeight="distnace in dp"> 
+0

不幸的是沒有工作。 –