有一个包含图像和文本视图的列表。我想做一个任务,即当用户点击某个项目时,更大的图像将显示在列表的右侧。
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.harshitbatra.baseadapter.MainActivity">
<LinearLayout
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:id="@+id/family_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<LinearLayout android:layout_width="0sp" android:layout_height="match_parent" android:layout_weight="2">
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:id="@+id/image"
android:src="@drawable/harshitbatra1"
android:layout_width="match_parent"
android:layout_height="150sp"/>
</FrameLayout>
</LinearLayout>
单击列表视图中的项目时,通过Toast输出是正确的,但它没有更新图像。 fImages数组是一个全局数组。这可能不使用碎片吗?
public class MainActivity extends AppCompatActivity
{
String[] fNames = {};
int[] fImages = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
ListView listView;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.family_list);
CustomAdapter myAdapter = new CustomAdapter(getApplicationContext(), fNames, fImages);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(clickListener);
}
private AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
LayoutInflater inflator = getLayoutInflater();
view = inflator.inflate(R.layout.activity_main, null);
image = (ImageView) view.findViewById(R.id.image);
Log.d("Image = "+image+" i = "+i,"OnClickListener");
//Toast.makeText(MainActivity.this, "Image = "+image+" i = "+i, Toast.LENGTH_SHORT).show();
image.setImageResource(fImages[i]);
Toast.makeText(MainActivity.this, "You Selected = "+fNames[i], Toast.LENGTH_SHORT).show();
}
};
/* Custom Adapter Implementation */
private class CustomAdapter extends BaseAdapter
{
String[] fNames;
int[] fImages;
LayoutInflater inflator;
public CustomAdapter(Context c, String[] fNames, int[] fImages)
{
this.fNames = fNames;
this.fImages = fImages;
this.inflator = (LayoutInflater.from(c));
}
@Override
public int getCount()
{
return fNames.length;
}
@Override
public Object getItem(int i)
{
return null;
}
@Override
public long getItemId(int i)
{
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
if(i%2 == 0)
{
view = inflator.inflate(R.layout.list_item_view,null);
}
else
{
view = inflator.inflate(R.layout.list_item_view_2,null);
}
ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView name = (TextView) view.findViewById(R.id.name);
// Log.d("Icon = "+icon,"getView");
//
// Log.d("Image = "+fImages[i], "getView");
icon.setImageResource(fImages[i]);
name.setText(fNames[i]);
return view;
}
}
}
答案 0 :(得分:0)
这里错了。 view
是您选择的项目视图。不要使用activity_main对其进行充气,只需删除那些行。
LayoutInflater inflator = getLayoutInflater();
view = inflator.inflate(R.layout.activity_main, null);
image = (ImageView) view.findViewById(R.id.image);
每次点击项目视图时,您都无需启动image
。在onCreate()
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.family_list);
// init image here
image = (ImageView) findViewById(R.id.image);
CustomAdapter myAdapter = new CustomAdapter(getApplicationContext(), fNames, fImages);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(clickListener);
}
希望它有所帮助!