我应该在哪里放置浮动操作按钮代码?

时间:2018-04-14 05:51:25

标签: android listview floating-action-button

我是Android编程的新手,我从互联网上获取此代码,我想修改它,因此它有浮动操作按钮,但每次我把java代码用于浮动操作按钮,它总是失败,可以有人告诉我应该把FAB的java代码放在哪里?这是xml和java代码

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.irfanliy.mycrud.TampilSemuaPgw">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/FAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="end|bottom"
    android:clickable="true"
    android:focusable="true"
    app:srcCompat="@android:drawable/ic_input_add" />

    </RelativeLayout>

Java:

package com.example.irfanliy.mycrud;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;



public class TampilSemuaPgw extends AppCompatActivity implements ListView.OnItemClickListener {


private ListView listView;

private String JSON_STRING;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tampil_semua_pgw);
    listView = (ListView) findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    getJSON();

}



private void showEmployee() {
    JSONObject jsonObject = null;
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);

        for (int i = 0; i < result.length(); i++) {
            JSONObject jo = result.getJSONObject(i);
            String id = jo.getString(konfigurasi.TAG_ID);
            String name = jo.getString(konfigurasi.TAG_NAMA);

            HashMap<String, String> employees = new HashMap<>();
            employees.put(konfigurasi.TAG_ID, id);
            employees.put(konfigurasi.TAG_NAMA, name);
            list.add(employees);
        }

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

    ListAdapter adapter = new SimpleAdapter(
            TampilSemuaPgw.this, list, R.layout.list_item,
            new String[]{konfigurasi.TAG_ID, konfigurasi.TAG_NAMA},
            new int[]{R.id.id, R.id.name});

    listView.setAdapter(adapter);
}

private void getJSON() {
    class GetJSON extends AsyncTask<Void, Void, String> {

        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(TampilSemuaPgw.this, "Mengambil Data", "Mohon Tunggu...", false, false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            JSON_STRING = s;
            showEmployee();
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
            return s;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, TampilPegawai.class);
    HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
    String empId = map.get(konfigurasi.TAG_ID).toString();
    intent.putExtra(konfigurasi.EMP_ID, empId);
    startActivity(intent);
}


}

1 个答案:

答案 0 :(得分:0)

就像在onCreate()方法

中初始化listview一样
    FloatingActionButton fab;
    fab=(FloatingActionButton) findViewById(R.id.FAB);
        fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

               //do something on click of it.
    }
});