如何将我的布局添加到我的画廊?

时间:2013-11-26 11:41:33

标签: android layout gallery adapter

是否可以填充图库而不是图片? 我正在尝试创建一个addapter,但我不能,这里是我的代码

<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=".MainActivity" >

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="134dp" />

 </RelativeLayout>

我希望将我的slide.xml添加到此 gallery1

我使用了这段代码,但它不起作用

  package com.slide;

   import com.slide.R.layout;

   import android.os.Bundle;
   import android.app.Activity;
   import android.content.Context;
   import android.support.v4.view.ViewPager;
   import android.text.Layout;
   import android.view.LayoutInflater;
   import android.view.Menu;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.BaseAdapter;
   import android.widget.Gallery;
   import android.widget.ImageView;
   import android.widget.TextView;

  public class MainActivity extends Activity {
Context context;

LayoutInflater inflater;

 String[] pics1 = {
            "Tous les Chantiers",
            "Chantier 1",
            "Chantier 2",
            "Chantier 3",
            "Chantier 4",
            "Chantier 5",

    }; 
 @SuppressWarnings("deprecation")

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     final  Gallery ga1 = (Gallery)findViewById(R.id.gallery1);
     ga1.setAdapter(new layoutAdapter1(this));


}

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


public class layoutAdapter1 extends BaseAdapter {

    private Context ctx;


    public TextAdapter1(Context c) {
        ctx = c;
    }

    @Override
    public int getCount() {

        return pics1.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        // Declare Variables
        TextView txtrank;
        TextView txtcountry;
        TextView txtpopulation;
        TextView imgflag;


        View itemView =  getLayoutInflater().inflate(R.layout.slide, arg2);


        ((Gallery) arg2).addView(itemView);

        return itemView;
    }}

  }

请帮帮我 :(

1 个答案:

答案 0 :(得分:0)

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    // Declare Variables
    TextView txtrank;
    TextView txtcountry;
    TextView txtpopulation;
    TextView imgflag;
    View itemView = (arg1 == null) ? View.inflate(ctx, R.layout.slide, null) : arg1;
    return itemView;
}
  1. Gallery不接受直接儿童
  2. 始终在适配器内使用ViewHolders
  3. 无需为每个getView来电创建观看次数。如果从Gallery传递的视图为null,则创建。