我试图放大最中心的列表视图项目。(最终,我希望将其扩展为一种简单而轻巧的 3D 效果,即元素在屏幕中间时会出现放大,在下方时会出现缩小,全黑背景和文本项会放大和缩小... )
所以,我制作了一个虚拟列表视图,每一行都是一个文本视图元素。下面,我有一个自定义适配器。
我所做的唯一相关修改是:
在 中OnCreate()
,我将以下变量传递给适配器:
int v = (listview.getLastVisiblePosition() - listview.getFirstVisiblePosition()) + 1;
在适配器的 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 中,我只有一个列表视图,而在“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:
完整的代码,仍然给出空异常。