在 Android 中的两个线性布局中创建的项目的无限滚动

Infinite Scrolling of items created in two Linear Layout in Android

我将屏幕分为两部分,一部分包含五个按钮,另一部分包含七个带图像的 ImageView。现在我希望 ImageView 无限旋转,意味着在最后一张图像之后,图像再次开始出现。我的XML是

      <LinearLayout 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="horizontal"
    android:baselineAligned="true"
    tools:context="in.example.splitapp.MainActivity" >
  <ScrollView 
                android:id="@+id/scrollView"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                 android:layout_weight="1" 
                 >
    <RelativeLayout 
        android:id="@+id/linearLayout1"
         android:orientation="vertical"
        android:layout_weight="1" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:background="@android:color/holo_green_dark">
        <Button android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:text="DOG"/>
         <Button android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
             android:layout_below="@id/button1"
            android:text="CAT"/>
          <Button android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/button2"
            android:text="COW"/>
           <Button android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/button3"
            android:text="RAT"/>
            <Button android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/button4"
            android:text="PARROT"/>
        </RelativeLayout>
        </ScrollView>
            <ScrollView 
                android:id="@+id/scrollView2"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                 android:layout_weight="1" 
                 >
    <RelativeLayout 
        android:id="@+id/linearLayout2"
        android:orientation="horizontal"
        android:layout_weight="1" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:background="@android:color/holo_purple"
       >

        <ImageView android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/dog"
            android:text="DOG"/>
         <ImageView android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
             android:layout_below="@id/imageView1"
             android:background="@drawable/cat"
            android:text="CAT"/>
          <ImageView android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/imageView2"
               android:background="@drawable/cow"
            android:text="COW"/>
           <ImageView android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/imageView3"
               android:background="@drawable/rat"
            android:text="RAT"/>
            <ImageView android:id="@+id/imageView5"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/imageView4"
               android:background="@drawable/parrot"
            android:text="PARROT"/>
             <ImageView android:id="@+id/imageView6"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/imageView5"
               android:background="@drawable/horse"
            android:text="HORSE"/>
              <ImageView android:id="@+id/imageView7"
            android:layout_width="match_parent"
            android:layout_height="100dp"
             android:layout_centerHorizontal="true"
               android:layout_below="@id/imageView6"
               android:background="@drawable/fish"
            android:text="FISH"/>
        </RelativeLayout>

</ScrollView>
</LinearLayout>

我希望 ImageView 横向滚动无限意味着这五个图像总是重复。

这是我的 MainActivity:

    public class MainActivity extends Activity implements OnScrollListener {

List<String> animalNameList = new ArrayList<String>();
ArrayList<Integer> animalImageList = new ArrayList<Integer>();
ImageAdapter imageAdapter;
NameAdapter nameAdapter = null;;
boolean flag = false;
ListView listView;
ListView listView1;
ListView upperListView;
RelativeLayout parentLayout;
LinearLayout childLayout;
int index = 0;
UpdateAdapter updateAdapter = null;
ArrayList<DataSplit> array = new ArrayList<DataSplit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
     animalNameList.add("CAT");
     animalNameList.add("DOG");
     animalNameList.add("COW");
     animalNameList.add("RAT");
     animalNameList.add("PARROT");
     animalNameList.add("HORSE");
     animalNameList.add("FISH");

     animalImageList.add(R.drawable.horse);
     animalImageList.add(R.drawable.parrot);
     animalImageList.add(R.drawable.fish);
     animalImageList.add(R.drawable.rat);
     animalImageList.add(R.drawable.dog);
     animalImageList.add(R.drawable.cat);
     animalImageList.add(R.drawable.cow);

     parentLayout = (RelativeLayout)findViewById(R.id.parentRelative);
     childLayout = (LinearLayout)findViewById(R.id.childLinearLayout);

     nameAdapter = new NameAdapter(MainActivity.this, 
                -1, animalNameList);
     listView = (ListView)findViewById(R.id.listView1);
    listView.setAdapter(nameAdapter);


      imageAdapter = new ImageAdapter(MainActivity.this, -1,      animalImageList);
     listView1 = (ListView)findViewById(R.id.listView2);
     CircularListAdapter circularAdapter = new     CircularListAdapter(imageAdapter);
    listView1.setDivider(null);
    listView1.setAdapter(circularAdapter);

    listView1.setOnScrollListener(this);



    upperListView = (ListView)findViewById(R.id.upperListView);


    listView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub


        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            if(flag)
            {



            }

        }
    });

}


class NameAdapter extends BaseAdapter {
    private List<String> animalName;
    private Activity context;

    public NameAdapter(Activity context, int textViewResourceId,
            List<String> animalName) {
        super();
        this.animalName = animalName;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater=context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.animalname, parent, false);
            Button button=(Button)convertView.findViewById(R.id.button);
            button.setId(position);
            button.setText(animalName.get(position));

        }
        return convertView;
    }

    public int getCount() 
    {
        int size = animalName.size();
        return size;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

}


class ImageAdapter extends ArrayAdapter<Integer> {
    private ArrayList<Integer> animalImage;
    private Activity context;

    public ImageAdapter(Activity context, int textViewResourceId,
            ArrayList<Integer> animalImage) {
        super(context, textViewResourceId);
        this.animalImage = animalImage;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if(view == null) {
            view = new View(MainActivity.this);
        }
        view.setId(position);
        //view.setBackgroundResource(animalImage[position]);
        view.setBackgroundResource(animalImage.get(position));
        return view;
    }

    public int getCount() 
    {
        int size = animalImage.size();
        return size;
    }

}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {


        int childInt = view.getChildAt(0).getId();
        //String img1Text = this.getResources().getResourceEntryName(animalImage[childInt]);
        String img1Text = this.getResources().getResourceEntryName(animalImageList.get(childInt));
        //String buttonText = animalName[index];
        String buttonText = animalNameList.get(index);
        if(img1Text.equalsIgnoreCase(buttonText))
        {
            final String name = animalNameList.get(index);
            int image = animalImageList.get(childInt);

            DataSplit data = new DataSplit();
            data.setAnimalName(name);
            data.setAnimalImage(image);
            array.add(data);


            index++;
            flag = true;
            if(array.size() == 1)
            {

                 updateAdapter = new UpdateAdapter(MainActivity.this,-1,array);
                 upperListView.setAdapter(updateAdapter);
                 upperListView.setVisibility(View.VISIBLE);

            }
            else
            {
                updateAdapter.notifyDataSetChanged();

            }


             listView.post(new Runnable() {

                    @Override
                    public void run() {
                        animalNameList.remove(name);
                        nameAdapter.notifyDataSetChanged();     
                    }
                });


        }



}


@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {

}

class UpdateAdapter extends ArrayAdapter<Integer> {

    ArrayList<DataSplit> dataSplit;
    private Activity context;

    public UpdateAdapter(Activity context, int textViewResourceId,
            ArrayList<DataSplit> dataSplit) {
        super(context, textViewResourceId);
        this.dataSplit = dataSplit;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
            LayoutInflater inflater=context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.addeddata, parent, false);
            Button button=(Button)convertView.findViewById(R.id.button);
            ImageView imageView=(ImageView)convertView.findViewById(R.id.imageView);                

            String animalName = dataSplit.get(position).getAnimalName();
            int imageName = dataSplit.get(position).getAnimalImage();

            button.setText(dataSplit.get(position).getAnimalName());
            imageView.setBackgroundResource(dataSplit.get(position).getAnimalImage());



        }
        return convertView;
    }

    public int getCount() 
    {
        int size = dataSplit.size();
        return size;
    }
}

}

我创建了 activity_main.xml,它包含一个父 RelativeLayout 和一个名为 upperListView 的 ListView,并且最初设置可见性消失了。在此之下,我创建了一个 LinearLayout,为此我为名称数据集和图像数据集创建了两个 ListView。当数据匹配时,我创建一个 UpdateAdapter,我在其中使用 addeddata.xml,其中创建了一个按钮和 ImageView,当它成功匹配时,隐藏的列表视图可见性变为可见,并且此 "addeddata.xml" 加载到列表视图中。这是我执行的。它会在第一次匹配时工作 perfectly.But,即 CAT 名称与 Cat 图像匹配,数据不会从列表视图和 CAT 名称中删除,再次显示在新的 ListView 下方。

如果应用程序崩溃,如何设法完成比赛。

这在过去已经讨论过,使用循环列表视图:

  1. How to create a closed (circular) ListView?

  2. Closed or circular Horizontal ListView Android

I just want when I scroll ImageView part then if Dog image matches with the right end side Dog name then image stick with the Dog Name Button and rest of the images remain scrolling.

为此,您可以尝试以下方法:

  • 滚动列表 1 和列表 2 中的项目后,遍历最短列表视图中的每个项目并检查 item1(position) [tag] = item2(position) [tag](所以,如果你有匹配)

  • 如果为真:

    1. 像这样重绘屏幕:在屏幕底部(两个列表视图下方)添加一个新的列表视图。这个列表视图同时包含 item1 和 item2(所以它的 litviewitem xml 将是一个线性布局,另外两个线性布局,一个包含匹配的宠物名称,另一个包含匹配的宠物图片)

    2. 从 listview1、listview2 中删除 item1(position) 和 item2(position)(这意味着您必须删除,例如,从 listview 宠物名称数据源中删除狗名,从 listview 宠物图片中删除狗图像数据源并将适配器重置为列表视图)

  • 这样您将继续能够滚动和匹配 listview1 和 listview2 中的项目 - 没有已经匹配的项目-,并且还有一个底部列表视图,其中包含所有无法匹配的匹配元素任何更多

希望对您有所帮助!

经过几天的时间,我找到了解决我问题的完美方法。我动态创建了 ImageView 和 ListView。图像数据正在 ListView 中填充。将文本与正确的图像匹配后,我从列表中取出该 ImageView 并设置为动态创建的 ImageView。以下是我的代码。

    public class MainActivity extends Activity implements OnScrollChangedListener, OnScrollListener{
int index = 0;
ImageView imageView1;
List<Integer> spectrum = new ArrayList<Integer>();
String[] animalName;
ImageView[] imageArray;
Button[] buttonArray;
ListView listView;
SpectrumAdapter spectrumAdapter;
ListView animalImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    animalName =  new String[]{"CAT","DOG","COW","RAT","PARROT","HORSE","FISH"};
    imageArray = new ImageView[animalName.length];
    buttonArray = new Button[animalName.length];        
    LinearLayout inner1 = (LinearLayout)findViewById(R.id.innerLayout1);
    LinearLayout inner2 = (LinearLayout)findViewById(R.id.innerLayout2);

    ListView animalList = (ListView)findViewById(R.id.animalList);

    NameAdapter nameAdapter = new NameAdapter(MainActivity.this, 
            -1, animalName);
    animalList.setAdapter(nameAdapter);
    for(int i=0;i<animalName.length;i++)
    {
        ImageView imageView = new ImageView(this);
        imageView.setId(i);
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,150);
        imageView.setTag(i);
        imageView.setLayoutParams(params1);
        imageView.setVisibility(View.GONE);
        imageArray[i] = imageView;
        inner1.addView(imageView);
    }
    ListView listView = new ListView(this);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    listView.setLayoutParams(param);
    inner2.addView(listView);
    spectrum.add(R.drawable.horse);
    spectrum.add(R.drawable.rat);
    spectrum.add(R.drawable.cow);
    spectrum.add(R.drawable.dog);
    spectrum.add(R.drawable.fish);
    spectrum.add(R.drawable.parrot);
    spectrum.add(R.drawable.cat);
    spectrumAdapter = new SpectrumAdapter(MainActivity.this, 
            -1, spectrum);

    CircularListAdapter circularAdapter = new CircularListAdapter(spectrumAdapter);
    listView.setAdapter(circularAdapter);
    listView.setOnScrollListener(this);
}


class NameAdapter extends BaseAdapter {
    private String[] animalName;
    private Activity context;

    public NameAdapter(Activity context, int textViewResourceId,
            String[] animalName) {
        super();
        this.animalName = animalName;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater=context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.animalname, parent, false);
        }
        Button button=(Button)convertView.findViewById(R.id.button);
        button.setText(animalName[position]);
        return convertView;

    }

    public int getCount() 
    {
        int size = animalName.length;
        return size;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

}

class SpectrumAdapter extends ArrayAdapter<Integer> {
    private List<Integer> spectrum;
    public SpectrumAdapter(Context context, int textViewResourceId,
            List<Integer> spectrum) {
        super(context, textViewResourceId, spectrum);
        this.spectrum = spectrum;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if(view == null) {
            view = new View(MainActivity.this);
        }
        view.setId(position);
        view.setBackgroundResource(spectrum.get(position));
        return view;
    }

    public int getCount() 
    {
        int size = spectrum.size();
        return size;
    }

}

@Override
public void onScrollChanged() {
    // TODO Auto-generated method stub

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

    int childInt = view.getChildAt(0).getId();
    String img1Text = this.getResources().getResourceEntryName(spectrum.get(childInt));
    String bb = animalName[index];
    if(bb.equalsIgnoreCase(img1Text))
    {
        ImageView img = imageArray[index];
        img.setBackgroundResource(spectrum.get(childInt));
        img.setVisibility(View.VISIBLE);
        index++;
    }
    else
    {
    }
}
}

这是问题的完美解决方案。谢谢大家的建议。