0

我有两个网格视图。(我给的名字)

  • 顶部网格视图:一些数字,如“123456123456”
  • 底部网格视图:包含从 0 到 9 的数字

最初,gridview 中的所有项目都是空的(类似于不可见的)。基于单击底部网格视图中的项目,将显示顶部网格视图中的项目。

例如:假设顶部网格视图中的项目是“123456123456”。最初一切都是不可见的。单击底部网格视图中的项目后,我需要在顶部网格视图中显示适当的数字。可以说,我点击了 1。我拥有“1”的网格视图项目应该是可见的。

我尝试了这个逻辑,但是出了点问题,我得到了一些奇怪的结果。

看看输出:

在此处输入图像描述

如果您在单击 1 时看到输入“123456123456”的上图,则所有 1 都将变为可见。当我点击 2 时,2 变得可见,甚至额外的 1 被分配给网格。我不知道为什么会这样。

这是我尝试过的代码。

主要活动 :

public class MainActivity extends Activity {
GridView grid_topNumbers;
GridView gridview_belownumbers;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gridview_belownumbers = (GridView)findViewById(R.id.gridview_belownumbers);
    grid_topNumbers = (GridView)findViewById(R.id.gridview_topnumbers);

    TopGrid topGridAdapter = new TopGrid(this);

    Constants.topNumbers = "123456123456";


    List<TopNumbersObject> topNumList = new ArrayList<TopNumbersObject>();
    TopNumbersObject topnumObj = null;

    for(int i=0;i< Constants.topNumbers.length();i++){
        topnumObj = new TopNumbersObject();
    if(i<Constants.topNumbers.length()-1)
    {
        topnumObj.number = (Constants.topNumbers.substring(i, i+1))+"";
    }
    else
    {
        topnumObj.number = (Constants.topNumbers.substring(i))+"";
    }
    topnumObj.isVisited =false;
    topNumList.add(topnumObj);
    }

    Constants.TopNumbersList = topNumList;

    grid_topNumbers.setAdapter(topGridAdapter);
    gridview_belownumbers.setAdapter(new CustomGridViewAdapter(this,topGridAdapter));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}
}

我的底部网格视图适配器:

public class CustomGridViewAdapter extends BaseAdapter {

Context cont;
LinearLayout.LayoutParams params = null;
TopGrid topAdapter = null;

public CustomGridViewAdapter(Context c,TopGrid topAdapter) {
    params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    cont = c;
    this.topAdapter = topAdapter;

    // type = Typeface.createFromAsset(cont.getAssets(),
    // "fonts/letters_fonttype.ttf");
}

@Override
public int getCount() {

    return 10;
}

@Override
public Object getItem(int arg0) {

    return arg0;
}

@Override
public long getItemId(int arg0) {

    return arg0;
}

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

    View v;
    TextView tv;

     // if it’s not recycled, initialize some
                                // attributes
        LayoutInflater li = (LayoutInflater) cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.bottom_grid, null);

        tv = (TextView) v.findViewById(R.id.bottom_grid_item_textView1);
        tv.setText(arg0+"");

        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                TextView clickedTextView = (TextView)arg0;
                String letter = clickedTextView.getText().toString();

                for(int i=0;i<Constants.topNumbers.length();i++)
                {
                    TopNumbersObject letterObject = Constants.TopNumbersList.get(i);
                     if(letterObject.number.equalsIgnoreCase(letter))
                    {
                         letterObject.isVisited = true;
                        if(Constants.topNumbers.toLowerCase().lastIndexOf(letter.toLowerCase()) == i)
                        {
                            topAdapter.notifyDataSetChanged();
                            break;
                        }
                    }


                }

                clickedTextView.setClickable(false);
                clickedTextView.setOnClickListener(null);
            }

        });


    return v;

}
}

我的顶级网格适配器:

public class TopGrid extends BaseAdapter {

Context cont;
LinearLayout.LayoutParams params = null;

public TopGrid(Context c) {
    params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    cont = c;

}

@Override
public int getCount() {

    return Constants.TopNumbersList.size();
}

@Override
public Object getItem(int arg0) {

    return arg0;
}

@Override
public long getItemId(int arg0) {

    return arg0;
}

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

    View v =convertView;
    TextView tv;
    TopNumbersObject topnoObject = (Constants.TopNumbersList.get(arg0));

    Holder h;
    if (v == null) // if it’s not recycled, initialize some  attributes
    { 
        h=new Holder();
        LayoutInflater li = (LayoutInflater) cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.top_numbers_item, null);

        h.tv = (TextView) v.findViewById(R.id.top_grid_item_textView1);
        v.setTag(h);
    } 
    else 
    {
        h=(Holder)v.getTag();
    }
    if(topnoObject.isVisited)
    {
        h.tv.setText(topnoObject.number);
    }
    Log.e("letters", topnoObject.number+ "  "+topnoObject.isVisited+"  "+arg0);
    return v;

}
private static class Holder
{
    TextView tv;
}
}

常量文件:

public class Constants {
public static boolean ISGRE;
public static String topNumbers = "1234567890";
public static List<TopNumbersObject> TopNumbersList = null;
public static int chances = 8;
}

下一个我的 xml 文件:

活动主:

<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:background="#000000" >

<GridView
    android:id="@+id/gridview_topnumbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:numColumns="12"
    android:stretchMode="columnWidth" >
</GridView>



<GridView
    android:id="@+id/gridview_belownumbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/gridview_topnumbers"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:numColumns="7"
    android:stretchMode="columnWidth" >
</GridView>

</RelativeLayout>

top_numbers_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/top_grid_item_textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textColor="#FF00FF" />

</LinearLayout>

bottom_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/bottom_grid_item_textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textColor="#FF0011"
    android:text="A" />
</LinearLayout>

谁能建议我哪里出错了。

4

1 回答 1

0

当我需要时,设法改变 topgrid 项目的可见性。

而不是这个

if(topnoObject.isVisited)
{
     h.tv.setText(topnoObject.number);
}

使用了下面的代码。

if(topnoObject.isVisited)
{
    h.tv.setVisibility(View.VISIBLE);
}
else
{
    h.tv.setVisibility(View.INVISIBLE); 
}
h.tv.setText(topnoObject.number);

但仍在等待更准确的答案

于 2013-10-11T02:25:52.990 回答