0

HI friends私はアンドロイドで初心者であり、あなたの助けが必要です。 私はアクティビティのListActivityを拡張しています。それにカスタムのActionBarを追加したいと思います。 友人の中にはListFragmentの使用を提案しましたが、できませんでした。 MainActivityのような他のアクティビティでカスタムActionBarを使用するには、カスタムActionBarアクティビティから拡張して、actionbarメソッドを呼び出しました。ListActivityでカスタムActionBarを使用する

タンク・ユー・オール。

ListActivity:

public class AlbumsActivity extends ListActivity { 

    // Connection detector 
    ConnectionDetector     cd; 

    // Alert dialog manager 
    AlertDialogManager     alert   = new AlertDialogManager(); 

    // Progress Dialog 
    private ProgressDialog    pDialog; 

    // Creating JSON Parser object 
    JSONParser       jsonParser  = new JSONParser(); 

    ArrayList<HashMap<String, String>> albumsList; 

    // albums JSONArray 
    JSONArray       albums   = null; 

    // albums JSON url 
    private static final String  URL_ALBUMS  = "http://api.androidhive.info/songs/albums.php"; 

    // ALL JSON node names 
    private static final String  TAG_ID   = "id"; 
    private static final String  TAG_NAME  = "name"; 
    private static final String  TAG_SONGS_COUNT = "songs_count"; 


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

     cd = new ConnectionDetector(getApplicationContext()); 

     // Check for internet connection 
     if (!cd.isConnectingToInternet()) { 
      // Internet Connection is not present 
      alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error", 
        "Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 

     // Hashmap for ListView 
     albumsList = new ArrayList<HashMap<String, String>>(); 

     // Loading Albums JSON in Background Thread 
     new LoadAlbums().execute(); 

     // get listview 
     ListView lv = getListView(); 

     /** 
     * Listview item click listener 
     * TrackListActivity will be lauched by passing album id 
     * */ 
     lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, int arg2, 
            long arg3) { 
       // on selecting a single album 
       // TrackListActivity will be launched to show tracks inside the album 
       Intent i = new Intent(getApplicationContext(), TrackListActivity.class); 

       // send album id to tracklist activity to get list of songs under that album 
       String album_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString(); 
       i.putExtra("album_id", album_id); 

       startActivity(i); 
      } 
     }); 
    } 


    /** 
    * Background Async Task to Load all Albums by making http request 
    * */ 
    class LoadAlbums extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(AlbumsActivity.this); 
      pDialog.setMessage("Listing Albums ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 


     /** 
     * getting Albums JSON 
     * */ 
     @Override 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      // getting JSON string from URL 
      String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET", 
        params); 

      // Check your log cat for JSON reponse 
      Log.d("Albums JSON: ", "> " + json); 

      try { 
       albums = new JSONArray(json); 

       if (albums != null) { 
        // looping through All albums 
        for (int i = 0; i < albums.length(); i++) { 
         JSONObject c = albums.getJSONObject(i); 

         // Storing each json item values in variable 
         String id = c.getString(TAG_ID); 
         String name = c.getString(TAG_NAME); 
         String songs_count = c.getString(TAG_SONGS_COUNT); 

         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_ID, id); 
         map.put(TAG_NAME, name); 
         map.put(TAG_SONGS_COUNT, songs_count); 

         // adding HashList to ArrayList 
         albumsList.add(map); 
        } 
       } else { 
        Log.d("Albums: ", "null"); 
       } 

      } 
      catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 


     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     @Override 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all albums 
      pDialog.dismiss(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 
        ListAdapter adapter = new SimpleAdapter(
          AlbumsActivity.this, albumsList, 
          R.layout.list_item_albums, new String[]{ TAG_ID, 
            TAG_NAME, TAG_SONGS_COUNT }, new int[]{ 
            R.id.album_id, R.id.album_name, R.id.songs_count }); 

        // updating listview 
        setListAdapter(adapter); 
       } 
      }); 

     } 

    } 

CustomActionBar:

public class CustomActionBar extends Activity { 

    public void actionbarMethod() { 

     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View actionBarView = inflater.inflate(R.layout.custom_actionbar, null); 

     ActionBar actionBar = getActionBar(); 

     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams 
       (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 

     final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ImageView btnmenu = (ImageView) findViewById(R.id.btnmenu); 

     btnmenu.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) { 
        drawerLayout.closeDrawer(Gravity.RIGHT); 
       } else { 
        drawerLayout.openDrawer(Gravity.RIGHT); 

       } 

      } 
     }); 

    } 

} 

MainActivity:

public class MainActivity extends CustomActionBar { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      actionbarMethod(); 
    .................. 
    } 
} 
+0

高さのレイアウトを使用しますすべてのレイアウトの先頭に48dpが表示されます。原色とクリックイベントを設定することができます。 –

答えて

0

ますToolbarのレイアウトを作成することができます。これは、特にカスタマイズできるように作られたActionBarの新しいバージョンです。

関連する問題