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

addChild() to object extending layout inside Listview item, working but not showing the child

我正在尝试制作一个显示带有自定义适配器的 ListView 的应用程序,其中此列表的每个项目都包含 class Track 的 object,它扩展了 RelativeView。

该应用程序允许用户将 TextView 拖动到 ListView 上。然后,计算出ListView的item在拖拽的ImageView下的位置,然后把这个ImageView添加到track的RelativeLayout中object.

我遇到的问题是,将项目拖动到 listView 项目的轨道 object 上后,TextView 被添加到那个 object 的 RelativeView 中,但它从未显示。使用调试器,我检查轨道 object 确实具有预期的 child.

我在这里做错了什么,之后我应该以某种方式更新 RelativeLayouts 吗?

为了方便起见,我用代码替换了布局中拖动 TextView 的代码,我只是放了一个按钮,单击该按钮会在 ListView 的位置 0 的轨道中添加一个文本视图。

为此使用此 ListView 的原因是因为我希望该列表可滚动。这是一个正确的近似值,还是我应该更好地使用一个 LinearLayout,在一个可滚动的容器中包含 Track objects?

非常感谢

赛道Class

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>

我终于找到了解决办法。问题出在引用 class Track 对象的方式上,我想放置视图。行

 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);

然后一切正常。