扩大listview中的中间元素 - android?

时间:2013-10-06 04:23:55

标签: android listview text android-listview zoom

我试图放大中心列表查看项目。 (最后,我希望将其扩展为一种简单而轻盈的3D效果,在屏幕中间放置元素时会放大,当它们位于下方时缩小,总放大的黑色背景和文本项目放大和缩小......)

所以,我做了一个虚拟列表视图,每一行都是一个textview元素。下面,我有一个自定义适配器。

我所做的唯一相关修改是:

OnCreate()中,我将以下变量传递给适配器:

int v = (listview.getLastVisiblePosition() - listview.getFirstVisiblePosition()) + 1;

在Adapter的getView()中,我得到了这个:

if (position == x/2) {
        textView.setHeight(100);
    }

无论如何这里是所有的代码,我已经跳过了进口:(它不是那么大!......大部分是非常普遍的)

MainActivity.java:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView listview = (ListView) findViewById(R.id.listview);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7","iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7","iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
            list.add(values[i]);
        }
        int v = (listview.getLastVisiblePosition() - listview.getFirstVisiblePosition()) + 1;


        final MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, v, R.layout.row, list);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {

            }

        });
    }
}

MySimpleArrayAdapter.java

 public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final int x;

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public MySimpleArrayAdapter(Context context, int a, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.x = a;
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);

        if (position == x/2) {
            textView.setHeight(100);
        }

        return rowView;
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}

此外,在我的activity_main.xml中,我只有一个listview,在“row.xml”中我只有一个textview ..

问题:为什么没有文字出现?我清楚地将列表值添加到textview中。

为什么最重要的元素是大而不是中间元素,我特意将它设置为中间元素?

这是我当前代码给出的屏幕:

LOGCAT错误 - KMDev的解决方案

10-06 17:54:02.529: E/AndroidRuntime(19596): FATAL EXCEPTION: main
10-06 17:54:02.529: E/AndroidRuntime(19596): java.lang.RuntimeException: Unable to start activity ComponentInfo{mple.finala/mple.finala.MainActivity}: java.lang.NullPointerException
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.os.Looper.loop(Looper.java:154)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.ActivityThread.main(ActivityThread.java:4624)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at java.lang.reflect.Method.invokeNative(Native Method)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at java.lang.reflect.Method.invoke(Method.java:511)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at dalvik.system.NativeStart.main(Native Method)
10-06 17:54:02.529: E/AndroidRuntime(19596): Caused by: java.lang.NullPointerException
10-06 17:54:02.529: E/AndroidRuntime(19596):    at mple.finala.MainActivity$2.onScroll(MainActivity.java:79)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1297)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.widget.AbsListView.setOnScrollListener(AbsListView.java:1286)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at mple.finala.MainActivity.onCreate(MainActivity.java:58)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.Activity.performCreate(Activity.java:4479)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
10-06 17:54:02.529: E/AndroidRuntime(19596):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
10-06 17:54:02.529: E/AndroidRuntime(19596):    ... 11 more

编辑2:

完整代码,仍然提供null异常。

http://codeviewer.org/view/code:373d

2 个答案:

答案 0 :(得分:6)

需要解决一些问题。

  1. 您需要使用ViewHolder模式。那些以及您真正需要了解的关于ListView的内容都在GoogleIO 2010 - The World of ListView视频中。

  2. 调整getView中项目的布局并不是一个好主意,因为这会对ListView使用的视图回收器产生负面影响。那也是在视频中。我真的鼓励你尝试找到一种不同的方式来显示你想要显示的内容。

  3. 设置文字

    1. 您将项目所需的x维度传递给适配器的构造函数,而不是为其提供实际的项目资源ID。如果传入正确的参数,ArrayAdapter将为您设置文本。有关使用字符串资源和默认提供的列表项布局的简单示例:

      String[] mData = getResources().getStringArray(R.array.list_examples);
      ArrayAdapter<String> mBaseAdapter = 
                  new ArrayAdapter<String>(context, 
                                           android.R.layout.simple_list_item_1,
                                           android.R.id.text1,
                                           mListData);
      
    2. 如果我是你,我只是让适配器设置文本并通过调用super来获取项目布局:

      @Override
      public View getView(int position, View convertView, ViewGroup viewGroup){
          convertView = super.getView(position,convertView, viewGroup);
          //other code here
      }
      
    3. 如果你真的想自己动手,那么它采用ViewHolder模式

          //This is the ViewHolder
          static class Holder{
              TextView mText;
          }
      
      
          @Override
          public View getView(int position, View convertView, ViewGroup viewGroup){
      
               convertView = super.getView(position, convertView, viewGroup);
      
               if (convertView == null){
                    // If you reach this, it most likely means you're not supplying the
                    // adapter the layout and textview resource it needs. You can
                    // just comment out your getView override and see if your text gets
                    // displayed.
               }
      
               Holder mHolder;
               if(convertView.getTag() == null){
                  mHolder = new Holder();
                  mHolder.mText = (TextView)convertView.findViewById(android.R.id.text1);
                  convertView.setTag(mHolder);
               } else {
                  mHolder = (Holder) convertView.getTag();
               }
      
               mHolder.mText.setText(getItem(position).toString());
          }
      

      关于getXVisibleItem方法

      // These are both implemented in AdapterView which means
      // they're flat list positions
      int firstVis = mList.getFirstVisiblePosition(); 
      int lastVis = mList.getLastVisiblePosition();
      
      /* This is the "conversion" from flat list positions to ViewGroup child positions */
      int count = lastVis - firstVis; 
      
       /*  getChildAt(pos) is implemented in ViewGroup and has a different meaning for 
       *  its position values. ViewGroup tracks visible items as children and is 0 indexed. 
       *  This means you'll have 0 - X positions where X is however many items it takes 
       *  to fill the visible area of your screen; usually less than 10. */
       View listItem = mList.getChildAt(count - ((int)count/2)); // This will get the middle 
                                                              // item for you to adjust as 
                                                              // you want.
      

      要在屏幕中间找到该项目,您只需要将您的可见项目计数并减去该计数的一半,如上所述。

      设置高度实施

      您需要在ListView上设置AbsListView.OnScrollListener,当滚动停止时,将调整应用于中间项。例如:

      mList.setOnScrollListener(new OnScrollListener(){
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState){
      
          }       
      
          @Override
          public void onScroll(AbsListView view, 
                               int firstVisibleItem, 
                               int visibleItemCount,
                               int totalItemCount){
      
              //Note that this is called when the scroll completes. 
              //It gives us the positions we want so just calculate
              //the middle item, grab it and adjust the height directly
              //or by increasing margins if the item layout_height is 
              //wrap_content. I'm not positive the margins do what you
              // need, but setting the height directly should.
              LinearLayout listItem = mList // <-- Replace LinearLayout with your layout
                            .getChildAt(visibleItemCount - ((int)visibleItemCount/2));
      
              //Make sure you use the right LayoutParams for the listItem Layout
              //If your listItem is a RelativeLayout for instance, you would use:
              // listItem.setLayoutParams(
              //      new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
              //                                      theHeightYouWantHere));
              listItem.setLayoutParams(
                      new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                                    theHeightYouWantHere)); 
      
              }
      });
      

      我已经对ListView和ExpandableListView做了一些tangentially related blog posts,它们有关于如何刷新使用childAt策略的可见项(示例代码只是更改项目的文本/背景颜色)的完整代码示例。请原谅个人博客链接及其样式(它是新的,需要更多工作:),但代码运行良好。

      编辑2:对于android.R.simple_list_item_1

      只要您使用TextView作为项目布局(android.R.simple_list_item_1是TextView),这就完全符合您的要求。如果您决定使用ViewGroup布局,则需要使用LayoutParams方法替换item.setHeight。

      mList.setOnScrollListener(new OnScrollListener(){
          @Override
          public void onScrollStateChanged(final AbsListView view, final int scrollState){
      
          }
      
          @Override
          public void onScroll(final AbsListView view, 
                               final int firstVisibleItem,
                               final int visibleItemCount,
                               final int totalItemCount){
              if (visibleItemCount != 0){
      
                   Log.i( TAG, "firstVisibleItem: "
                               + firstVisibleItem
                               + "\nvisibleItemCount: "
                               + visibleItemCount);
      
                   final int midPosition = visibleItemCount - (visibleItemCount / 2); 
                   final TextView listItem = (TextView) mList.getChildAt(midPosition);
                   listItem.setHeight(500); //Only works if you're using
                                            //android.R.simple_list_item_1 since it's
                                            //not a ViewGroup, but rather a TextView
      
                   int count = visibleItemCount;
                   while (count >= 0){ // Here we need to loop through and make sure
                                       // all recycled items are returned to their 
                                       // original height. 
                       final TextView item = (TextView) mList.getChildAt(count);
                       if (item != null && count != midPosition){
                           item.setHeight(100);
                       }
                       count--;
                    }
              }
          }
      });
      

答案 1 :(得分:0)

第一个没有文字出现

在MySimpleArrayAdapter上,使用getView方法。你忘了为textView设置textTxt。这就是没有文字显示的原因。

第二个让中间项更大

在设置listview适配器之前尝试getLastVisiblePosition。我猜v的值必须为0,位置== 0.这就是为什么topper项目变大了。如果您尝试将其更改为位置== 1或2,则第二个或第三个项目可能会变大。

为了使中间项变大,setOnScrollLinstener为listview

public int currentLargedPosition = 0 // global value

listView.setOnScrollListener(new OnScrollListener(){

    @Override
    public void onScroll(AbsListView arg0, int firstVisibleItem, int visibleItemCount, int totalItemCount){
        Log.i("Scroll","first "+firstVisibleItem+", visibleItemCount "+visibleItemCount+",totalCount "+totalItemCount); 
        int center = (visibleItemCount)/2 + firstVisibleItem;
        if(currentLargedPosition != center){
            enlargeMiddleView(currentLargedPosition-firstVisibleItem, center-firstVisibleItem);
            currentLargedPosition = center;
        }

    }

    @Override
    public void onScrollStateChanged(AbsListView arg0, int arg1) {
        Log.i("scroll","onScrollStateChanged");             
    }
});

void enlargeMiddleView(int oldPosition, int newPosition){       

    // get enlarged view and make it return default size
    TextView newTextView = (TextView)listView.getChildAt(oldPosition).findViewById(R.id.text);
    newTextView.setHeight(50);

    // get the current center view and make it bigger
    TextView oldTextView = (TextView)listView.getChildAt(newPosition).findViewById(R.id.text);
    oldTextView.setHeight(300);     
}