【发布时间】:2016-05-25 09:47:20
【问题描述】:
我正在尝试制作一个显示带有自定义适配器的 ListView 的应用程序,其中此列表的每个项目都包含 Track 类的对象,它扩展了 RelativeView。
该应用程序允许用户将 TextViews 拖到 ListView 上。然后计算ListView的item在拖拽的ImageView下的位置,然后将此ImageView添加到track对象的RelativeLayout中。
我遇到的问题是,将项目拖到 listView 项目的 Track 对象上后,TextView 被添加到该对象的 RelativeView,但它从未显示。通过调试器,我检查了 track 对象是否确实具有预期的子对象。
我在这里做错了什么,之后我应该以某种方式更新RelativeLayouts吗?
为了方便起见,我用代码代替了布局中 TextView 的拖动,并且我只是放置了一个按钮,当单击该按钮时,它将在 ListView 的位置 0 的 Track 中添加一个文本视图。
为此使用此 ListView 的原因是因为我希望该列表可滚动。这是一个正确的近似值,还是我应该在可滚动容器内更好地使用包含 Track 对象的 LinearLayout?
非常感谢
轨道类
public class Track extends RelativeLayout {
static public ArrayList<Track> trackList = new ArrayList<>();
public ArrayList<Light> lightsOnThisTrackList = new ArrayList<>();
Context context;
private String name;
LayoutInflater mInflater;
public Track(Context _context) {
super(_context);
context = _context;
mInflater = LayoutInflater.from(_context);
init();
}
public Track(Context _context, AttributeSet attrs, int defStyle)
{
super(_context, attrs, defStyle);
context = _context;
mInflater = LayoutInflater.from(_context);
init();
}
public Track(Context _context, AttributeSet attrs) {
super(_context, attrs);
context = _context;
mInflater = LayoutInflater.from(_context);
init();
}
public void init() {
mInflater.inflate(R.layout.track_view, this, true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
final Track thisTrack = this;
this.context = context;
ImageView trackView = new ImageView(context);
trackView.setImageResource(R.drawable.track);
this.addView(trackView);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
直到这里,drawable.track 显示在 ListView 的每个项目中。
适配器:
public class TrackListAdapter extends ArrayAdapter<Track> {
private static final String TAG = "TrackListAdapter";
private LayoutInflater layoutInflater;
ArrayList<Track> trackArrayList;
Context mContext;
public TrackListAdapter(Context context, int textViewResourceId, ArrayList<Track> _trackArrayList) {
super(context, 0, _trackArrayList);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mContext = context;
this.trackArrayList = _trackArrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.track_list_item, null);
}
if (trackArrayList.size() != 0) {
Track track = trackArrayList.get(position);
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(track.getName()); //This is working and gets updated
}
return convertView;
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
RelativeLayout relativeLayout;
Context context;
ImageButton newItemButton ;
Button button;
public ListView trackListView;
TrackListAdapter trackListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
relativeLayout = (RelativeLayout) findViewById(R.id.mainLayout);
trackListView = (ListView) findViewById(R.id.trackListView);
trackListAdapter = new TrackListAdapter(this, 0, Track.trackList);
trackListView.setAdapter(trackListAdapter);
//This button adds new track to the list
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Track.trackList.add(new Track(context));
Log.i(TAG, "onClick " + Track.trackList.size());
trackListAdapter.notifyDataSetChanged();
}
});
//This button adds a new TextView to the track object of the item(0)
//of the ListView. The view is added, according to the debugger,
//but it is not shown
newItemButton = (ImageButton) findViewById(R.id.newItemButton );
newItemButton .setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Track track = trackListAdapter.getItem(0);
track = (Track) findViewById(R.id.view);
RelativeLayout.LayoutParams imParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView text = new TextView(context);
text.setText("hello world!");
track.addView(text, imParams);
trackListAdapter.notifyDataSetChanged();
return false;
}
});
}
Track_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
track_list_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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView" />
<com.example.microinnova.smartcontrol.Track
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view" />
</LinearLayout>
【问题讨论】:
-
通过查看您的代码,您实际上并没有更改适配器的基础数据 - 因此调用
trackListAdapter.notifyDataSetChanged()没有任何效果。您需要在适配器中添加一个方法,该方法添加一个Track(到ArrayList<Track> trackArrayList)。添加类似public void addItem(Track item){ trackArrayList.add(item); }的方法 -
有
Track.trackList.add(new Track(context))行和Track.trackList绑定到new TrackListAdapter(this, 0, Track.trackList)行中的适配器。我认为这不是问题,因为在单击button后,listView 会显示新的 Track。问题是,当单击newItemButton(应该修改列表中的一个 Track 对象)时,该对象在视觉上没有发生任何事情。
标签: android listview android-custom-view android-adapter android-relativelayout