如何设置Android网格视图自定义适配器的布局

时间:2016-04-05 10:47:15

标签: android android-layout gridview adapter

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.activity.MainActivity;
import com.nusecond.suredeal.app.suredeal.activity.Promo;
import com.nusecond.suredeal.app.suredeal.pojo.Campaigns;

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

/**
 * Created by ns2 on 2/4/16.
 */
public class CustomPromoAdapter extends BaseAdapter{
    private Activity activity;
    private LayoutInflater inflater;
    private List<Campaigns> campaigns;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomPromoAdapter(Activity activity, List<Campaigns> campaigns){
        this.activity=activity;
        this.campaigns=campaigns;
    }

    @Override
    public int getCount() {
        return campaigns.size();
    }

    @Override
    public Object getItem(int location) {
        return campaigns.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater==null)
            inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView==null)
            convertView=inflater.inflate(R.layout.promo_gridlist,null);
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail1);

        TextView tView = (TextView)convertView.findViewById(R.id.textView1);
      //  tView.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
        Campaigns ca=campaigns.get(position);
        tView.setText(ca.getName());
        ImageView imgView = (ImageView)convertView.findViewById(R.id.imageView1);
        thumbNail.setImageUrl(ca.getImage(), imageLoader);

        return convertView;
    }
}
  1. 这是我的自定义适配器,用于在网格视图中显示图像。
  2. 现在我只显示4张图片但我需要8张。
  3. 文本视图也显示不正确。
  4. 我不知道我需要在哪里更改我的意思是在我的适配器或我的布局文件中。
  5. 我试图设置LayoutParms但它无法正常工作。
  6. 我是Android的新手可以帮助我......
  7. 下面是我的布局....
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:orientation="vertical" >
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/thumbnail1"
            android:layout_width="88dp"
            android:layout_gravity="center"
            android:layout_height="88dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"/>
    
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_gravity="center"
            android:layout_width="88dp"
            android:layout_height="88dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:text="TextView" />
    
    </LinearLayout> 

    1. 我从Rest Setver获取图片和文字..
    2. 下面是我的主要布局....
    3. <?xml version="1.0" encoding="utf-8"?>
      <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:orientation="vertical"
      
          tools:context=".suredeal.activity.Promo" >
      
          <GridView
              android:id="@+id/gridView1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/textView1"
              android:numColumns="2" >
      
          </GridView>
      
      
      
      </RelativeLayout>

      1. 这是我的主要版面..
      2. This is What I'm Displaying With The Above Code..

        This is What I'm Trying To Do...

        1. 任何帮助感谢他们.......

0 个答案:

没有答案