0

我已按照此链接了解如何实现自定义微调器。我的要求只是在单击图像时获得下拉菜单,它应该像一个下拉菜单,在其中我将内容从后端加载到列表中。

但我认为上面链接中发布的示例示例会起作用。但它没有在单击图像图标时给出任何列表。

这是完整的源代码。

package com.example.testdroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;

public class MainActivity extends SherlockActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        createCustomActionBar();
    }

//  @Override
//  public boolean onCreateOptionsMenu(Menu menu) {
//      // Inflate the menu; this adds items to the action bar if it is present.
//      getMenuInflater().inflate(R.menu.activity_main, menu);
//      return true;
//  }

    private void createCustomActionBar() 
    {
        List<String> links = new ArrayList<String>();
        links.add("Abc");
        links.add("Def");
        links.add("Ghi");
        LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.externallink, links);

        View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
        IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
        spinner.setAdapter(linkAdapter);


        ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
        settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
        getSupportActionBar().setDisplayShowCustomEnabled(true);

    }

    private static class LinksAdapter extends ArrayAdapter<String> {

        private List<String> strings;
        private Context context;

        private LinksAdapter(Context mainActivity, int textViewResourceId, List<String> objects) {
            super(mainActivity, textViewResourceId, objects);
            this.strings = objects;
            this.context = mainActivity;
        }

        @Override
        public int getCount() {
            if (strings == null) return 0;
            return strings.size();
        }

        @Override
        public String getItem(int position) {
            return super.getItem(position);
        }


        // return views of drop down items
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {

            final String siteLink = strings.get(position);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // at 0 position show only icon

            TextView site = (TextView) inflater.inflate(R.layout.externallink, null);
            site.setText(siteLink);

            site.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
                    //context.startActivity(i);
                }
            });
            return site;


        }


        // return header view of drop down
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(R.layout.custom_show_action_bar, null);
        }
    }


}

这是我的 XML 文件,即布局 xml 文件。

custom_show_action_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
               android:gravity="right"
        >


    <com.actionbarsherlock.internal.widget.IcsSpinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingRight="20dp"
        android:layout_gravity="center"
            />

     <ImageView  android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:src="@drawable/ic_launcher"
                 android:paddingRight="20dp"
                 android:paddingLeft="10dp"
                 android:layout_gravity="center"
                 android:background="#ffffff"
                 android:id="@+id/refresh"/>


    <ImageView  android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/ic_launcher"
                android:paddingRight="20dp"
                android:background="#ffffff"
                android:layout_gravity="center"
                android:id="@+id/settings"/>

</LinearLayout>

外部链接.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="fill_parent"
         android:layout_height="wrap_content"
        />
</LinearLayout>

指导我哪里错了。

4

0 回答 0