2

我正在为我正在制作的应用程序制作菜单列表。我按照本教程创建它(根据需要更改值) - http://www.ezzylearning.com/tutorial.aspx?tid=1763429

现在我的清单不是很复杂。每次都会有相同的数据,而且只有两个部分。到目前为止,我的列表正常工作,但我需要用 xml 中的不同视图/布局替换第 7 项或添加该视图。

当前代码....

主要活动:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;


public class HU_main extends Activity {

    private ListView f_menu;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hu_main);

        Front_menu front_menu_data[] = new Front_menu[] {
            new Front_menu("Cadences", "Reference chart of all the primary cadences."),
            new Front_menu("Chords and Non-Chord Tones", "Reference chart of chord types and non-chord tones and associated rules of NCTs."),
            new Front_menu("Key Signatures", "Reference chart for key signature rules."),
            new Front_menu("Scale/Mode Triads", "List of triads and degrees for all standard diatonic scale modes."),
            new Front_menu("Harmonic Progression", "Reference chart for scale and mode chord progressions."),
            new Front_menu("Terminology Translation", "Reference list if primary music terms and instruments in German, Italian and French."),
            new Front_menu("Replace", "Replace"),
            new Front_menu("Chord Finder", "Enter notes to find chords."),
            new Front_menu("Modulation", "Reference chart of chord types and non-chord tones and associated rules of NCTs."),
            new Front_menu("Scale Finder", "Find scales and modes by notes."),
            new Front_menu("Transposition", "Show changes to and from concert pitch for transposing instruments or determine overall transposition of key.")
            };

            Front_menuAdapter adapter = new Front_menuAdapter(this,
            R.layout.item_layout, front_menu_data);

            f_menu = (ListView)findViewById(R.id.f_menu);

            f_menu.setAdapter(adapter);


        }

    }

Front_menu适配器:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class Front_menuAdapter extends ArrayAdapter<Front_menu> {
    Context context;
    int layoutResourceId;
    Front_menu data[] = null;



    public Front_menuAdapter(Context context, int layoutResourceId, Front_menu[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    Front_menuHolder holder = null;


        if (row == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new Front_menuHolder();
            holder.txtTitle1 = (TextView)row.findViewById(R.id.menu_item);
            holder.txtTitle2 = (TextView)row.findViewById(R.id.item_description);
            holder.header2 = (TextView)row.findViewById(R.id.tools_header);

            row.setTag(holder);


            if (position != 6) {
                holder.txtTitle1.setVisibility(View.VISIBLE);
                holder.txtTitle2.setVisibility(View.VISIBLE);
                holder.header2.setVisibility(View.GONE);
            }

            else if (position == 6) {
                holder.header2.setVisibility(View.VISIBLE);
                holder.txtTitle1.setVisibility(View.GONE);
                holder.txtTitle2.setVisibility(View.GONE);
            }

        }

        else {
            holder = (Front_menuHolder)row.getTag();
        }


        Front_menu front_menu = data[position];
        holder.txtTitle1.setText(front_menu.item);
        holder.txtTitle2.setText(front_menu.desc);

        return row;
    }

    class Front_menuHolder {
        TextView txtTitle1;
        TextView txtTitle2;
        TextView header2;
    }

}


Front_menu (constructor):

    public class Front_menu {
        public String item;
        public String desc;
        public Front_menu() {
             super();
        }

        public Front_menu(String item, String desc) {
            super();
            this.item = item;
            this.desc = desc;
        }

    }
4

1 回答 1

1

与其担心View为一项以不同方式显示而增加新的,为什么不在layout当前 XML 中定义另一项呢?

以我的应用程序中的一段实际代码为例,它显示了用户的 Facebook 新闻源(我已将其缩短并从中删除了不需要的代码)。用户可以在他/她的新闻源上拥有状态更新/照片或视频。因此,我没有创建多个布局 XML,而是将它们全部定义在一个 XML 中,然后在adapter.

XML 示例:

    <!-- STATUS | PHOTO -->

    <LinearLayout
        android:id="@+id/linlaStatusPhoto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        .... OTHER ELEMENTS NEEDED

    </LinearLayout>


    <!-- VIDEO LAYOUT -->

    <LinearLayout
        android:id="@+id/linlaVideo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        .... OTHER ELEMENTS NEEDED

    </LinearLayout>

现在在适配器中,我检查提要类型的条件并显示必要的布局,同时隐藏另一个。

if (feedType.equals("status") || feedType.equals("photo"))  {

    // SHOW THE NECESSARY LAYOUT CONTAINER
    holder.linlaStatusPhoto.setVisibility(View.VISIBLE);

    // HIDE THE OTHERS
    holder.linlaVideo.setVisibility(View.GONE);

  // I HAVE MORE CONDITIONS TO CHECK. HENCE THE else if().
} else if (feedType.equals("video")) {

    // SHOW THE NECESSARY LAYOUT CONTAINER
    holder.linlaVideo.setVisibility(View.VISIBLE);

    // HIDE THE OTHERS
    holder.linlaStatusPhoto.setVisibility(View.GONE);

}

您可以轻松地调整此代码以适应您的方案。您将需要使用一些检查来确定Widget必须显示哪些内容。但即使你要给另一个充气也是如此View。如果它始终是列表中的第 7 项,您可以再添加一个参数来front_menu_data[]保存项目编号并使用它来触发上述示例。

更新:

从这里移动if...else代码块:

if (row == null) {
    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);

    holder = new Front_menuHolder();
    holder.txtTitle1 = (TextView)row.findViewById(R.id.menu_item);
    holder.txtTitle2 = (TextView)row.findViewById(R.id.item_description);
    holder.header2 = (TextView)row.findViewById(R.id.tools_header);

    row.setTag(holder);

    // MOVE THIS BLOCK
    if (position != 6) {
        holder.txtTitle1.setVisibility(View.VISIBLE);
        holder.txtTitle2.setVisibility(View.VISIBLE);
        holder.header2.setVisibility(View.GONE);
    } else if (position == 6) {
        holder.header2.setVisibility(View.VISIBLE);
        holder.txtTitle1.setVisibility(View.GONE);
        holder.txtTitle2.setVisibility(View.GONE);
    }

} else {
    holder = (Front_menuHolder)row.getTag();
}

至:

Front_menu front_menu = data[position];

if (position != 6) {
    holder.txtTitle1.setVisibility(View.VISIBLE);
    holder.txtTitle2.setVisibility(View.VISIBLE);
    holder.header2.setVisibility(View.GONE);

    holder.txtTitle1.setText(front_menu.item);
    holder.txtTitle2.setText(front_menu.desc);

} else if (position == 6) {
    holder.header2.setVisibility(View.VISIBLE);
    holder.txtTitle1.setVisibility(View.GONE);
    holder.txtTitle2.setVisibility(View.GONE);

    holder.txtTitle1.setText(front_menu.item);
    holder.txtTitle2.setText(front_menu.desc);
}
于 2013-04-22T04:15:44.377 回答