addChild()到Listview项目内的对象扩展布局,工作但不显示子项

时间:2016-05-25 09:47:21

标签: android listview android-custom-view android-adapter android-relativelayout

我正在尝试创建一个显示带有自定义适配器的ListView的应用程序,其中此列表的每个项目都包含类Track的对象,该对象扩展了RelativeView。

该应用程序允许用户在ListView上拖动TextViews。然后,它在拖动的ImageView下计算ListView项的位置,然后将此ImageView添加到轨道对象的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>

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。问题在于引用类的对象轨道我想放置视图。行

 Track track = trackListAdapter.getItem(0);
        track = (Track) findViewById(R.id.view);

确实无所作为,因为第一个检索到的轨道后来被通用布局R.id.view的内容所取代。 我找到了这段很好的代码here,它可以检索ListView中特定位置的项目视图。

  public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

然后我的方法是:

        View view = trackListAdapter.getViewByPosition(trackNumber, trackListView);
        Track track = (Track) view.findViewById(R.id.view);

        track.addView(viewToAdd);

然后一切都按预期工作。