我在电视上实现了Android应用。在电视上,我们可以通过遥控器上的箭头按钮导航项目。问题是:我有一个ListView,当我浏览列表时,我想在聚焦或选择时使项目更大。 这是我的演示代码:
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
private static String TAG = "MainActivity";
private String[] mMonths = {"January" , "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
private ListView mListView;
private CustomAdapter mAdapter;
private View mPre = null;
private int mHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> list = new ArrayList<String>(Arrays.asList(mMonths));
mAdapter = new CustomAdapter(this, list);
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(mAdapter);
mListView.setOnItemSelectedListener(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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Log.e(TAG, "onItemSelected position " + position);
LayoutParams params = (LayoutParams) view.getLayoutParams();
float height = getResources().getDisplayMetrics().density * 80;// Bigger height
params.height = (int) height;
view.invalidate();
if(mPre != null){
height = getResources().getDisplayMetrics().density * 50; //Default height
params = (LayoutParams) mPre.getLayoutParams();
params.height = (int) height;
}
mPre = view;
mPre.invalidate();
mListView.invalidate();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
Log.e(TAG, "onNothingSelected");
}
我的CustomAdpater.java
public class CustomAdapter extends BaseAdapter{
private ArrayList<String> mMonths;
private Context mContext;
public CustomAdapter(Context context, ArrayList<String> arrays){
mContext = context;
mMonths = arrays;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mMonths.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mMonths.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null)
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
((TextView) convertView.findViewById(R.id.tv)).setText(mMonths.get(position));
convertView.setTag(position);
return convertView;
}
}
我的item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/lv_selector"
android:gravity="center">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:gravity="center"
android:textSize="20sp"
android:background="@color/white"/>
</LinearLayout>
我不知道为什么它不起作用。它不会更改所选项目和上一项目的高度。 任何帮助将不胜感激。
答案 0 :(得分:0)
我解决了我的问题。不要使用View mPre
,从mListView.getChildAt()
获取子视图
这是我更新的代码,适合任何人需要它。我还制作了平滑的动画。
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener, OnFocusChangeListener {
private static String TAG = "MainActivity";
private String[] mMonths = {"January" , "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
private ListView mListView;
private CustomAdapter mAdapter;
private int mPrePos = -1;
private SmoothAnimation mSmoothRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> list = new ArrayList<String>(Arrays.asList(mMonths));
mAdapter = new CustomAdapter(this, list);
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(mAdapter);
mListView.setOnItemSelectedListener(this);
mListView.setOnFocusChangeListener(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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Log.e(TAG, "onItemSelected position " + position);
float height;
LayoutParams params;
int firstVisiblePos = mListView.getFirstVisiblePosition();
if(mPrePos != -1 && mListView.getChildAt(mPrePos - firstVisiblePos) != null){
View child = mListView.getChildAt(mPrePos - firstVisiblePos);
height = getResources().getDisplayMetrics().density * 50;
params = (LayoutParams) child.getLayoutParams();
params.height = (int) height;
child.requestLayout();
}
mPrePos = position;
params = (LayoutParams) view.getLayoutParams();
height = getResources().getDisplayMetrics().density * 100;
// params.height = (int) height;
view.post(new SmoothAnimation(view));
view.requestLayout();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
Log.e(TAG, "onNothingSelected");
int firstVisiblePos = mListView.getFirstVisiblePosition();
if(mPrePos != -1 && mListView.getChildAt(mPrePos - firstVisiblePos) != null){
View child = mListView.getChildAt(mPrePos - firstVisiblePos);
float height = getResources().getDisplayMetrics().density * 50;
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.height = (int) height;
child.requestLayout();
mPrePos = -1 ;
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
int firstVisiblePos = mListView.getFirstVisiblePosition();
if(v.getId() == R.id.list && hasFocus == false && mPrePos != -1 && mListView.getChildAt(mPrePos - firstVisiblePos) != null){
View child = mListView.getChildAt(mPrePos - firstVisiblePos);
float height = getResources().getDisplayMetrics().density * 50;
LayoutParams params = (LayoutParams) child.getLayoutParams();
params.height = (int) height;
child.requestLayout();
mPrePos = -1 ;
}
}
private class SmoothAnimation implements Runnable{
private View mChild;
private float maxHeight;
private float minHeight;
private Interpolator mInterpolator = new LinearInterpolator();
private final long mDuration = 1000;
private boolean mContinueRunning = true;
private long mStartTime = -1;
private float mCurrentHeight = -1;
public SmoothAnimation(View child){
mChild = child;
maxHeight = mChild.getContext().getResources().getDisplayMetrics().density * 100;
minHeight = mChild.getContext().getResources().getDisplayMetrics().density * 50;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(mStartTime == -1){
mStartTime = System.currentTimeMillis();
Log.e(TAG, "current time " + mStartTime);
}
else{
Log.e(TAG, "current time 1 " + System.currentTimeMillis() + " " + mStartTime);
long nomarlizedTime = 1000 * (System.currentTimeMillis() - mStartTime) / mDuration;
nomarlizedTime = Math.max(Math.min(nomarlizedTime, 1000), 0);
float interporlatedHeight = (maxHeight - minHeight) * mInterpolator.getInterpolation(nomarlizedTime / 1000f);
LayoutParams params = (LayoutParams) mChild.getLayoutParams();
mCurrentHeight = minHeight + interporlatedHeight;
Log.e(TAG, "height" + mCurrentHeight);
params.height = (int) mCurrentHeight;
mChild.requestLayout();
}
if(mContinueRunning && mCurrentHeight != maxHeight){
mChild.postDelayed(this, 16);
}
}
}
}