答案 0 :(得分:3)
<强> AdapterMain.java 强>
package com.demoproject;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
/**
* Created by raghav on 10-03-2016.
*/
public class AdapterMain extends RecyclerView.Adapter<AdapterMain.DataObjectHolder> {
private static MyClickListener myClickListener;
private ArrayList<ModelMain> modelMains;
public AdapterMain(ArrayList<ModelMain> modelMains) {
this.modelMains = modelMains;
}
public void setOnItemClickListener(MyClickListener myClickListener) {
AdapterMain.myClickListener = myClickListener;
}
@Override
public int getItemCount() {
return modelMains.size();
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_main_list, parent, false);
return new DataObjectHolder(view);
}
@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
holder.listenerService.updatePosition(position);
holder.listenerPrice.updatePosition(position);
holder.edService.setText(modelMains.get(position).getService());
holder.edPrice.setText("" + modelMains.get(position).getPrice());
if (modelMains.get(position).isBtnPlus()){
holder.btnAdd.setText("+");
holder.btnAdd.setBackground(holder.itemView.getContext().getResources().getDrawable(R.drawable.button_design_plus));
}else {
holder.btnAdd.setText("-");
holder.btnAdd.setBackground(holder.itemView.getContext().getResources().getDrawable(R.drawable.button_design_minus));
}
}
public void addItem(ModelMain modelMain, int index) {
modelMains.add(modelMain);
if (index !=0 ){
modelMains.get(index-1).setBtnPlus(false);
}
notifyDataSetChanged();
}
public void deleteItem(int index) {
if (modelMains.size() > index){
modelMains.remove(index);
notifyDataSetChanged();
totalPrice();
}
}
public interface MyClickListener {
void onItemClick(int position, View v);
}
private class MyCustomEditTextListener implements TextWatcher {
private int position;
private EditText editText;
public MyCustomEditTextListener(EditText ed) {
this.editText = ed;
}
public void updatePosition(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (charSequence.length() == 0){
return;
}
if (editText.getId() == R.id.txtPrice){
if (String.valueOf(modelMains.get(position).getPrice()).equals(charSequence.toString())){
return;
}else {
modelMains.get(position).setPrice(Integer.parseInt(charSequence.toString()));
totalPrice();
}
}else if (editText.getId() == R.id.txtService){
if (modelMains.get(position).getService().equals(charSequence.toString())){
return;
}else {
modelMains.get(position).setService(charSequence.toString());
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
}
private void totalPrice() {
int price=0;
for (int j = 0;j<modelMains.size();j++){
price+=modelMains.get(j).getPrice();
}
MainActivity.txtTotalPrice.setText(""+price);
}
public class DataObjectHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
EditText edService, edPrice;
Button btnAdd;
LinearLayout linBtn;
MyCustomEditTextListener listenerPrice,listenerService;
public DataObjectHolder(View itemView) {
super(itemView);
edService = (EditText) itemView.findViewById(R.id.txtService);
edPrice = (EditText) itemView.findViewById(R.id.txtPrice);
btnAdd = (Button) itemView.findViewById(R.id.btnAdd);
linBtn = (LinearLayout)itemView.findViewById(R.id.linBtn);
linBtn.setOnClickListener(this);
btnAdd.setOnClickListener(this);
this.listenerPrice = new MyCustomEditTextListener(this.edPrice);
this.listenerService = new MyCustomEditTextListener(this.edService);
this.edPrice.addTextChangedListener(listenerPrice);
this.edService.addTextChangedListener(listenerService);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getLayoutPosition(), v);
}
}
}
<强> MainActivity.java 强>
package com.demoproject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView tableList;
ArrayList<ModelMain> modelMains;
private AdapterMain adapterMain;
private Toolbar toolbar;
public static TextView txtTotalPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Create An Initial Invoice");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tableList = (RecyclerView) findViewById(R.id.tableList);
tableList.setLayoutManager(new LinearLayoutManager(this));
txtTotalPrice = (TextView)findViewById(R.id.txtTotalPrice);
findViewById(R.id.btnNext).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,HomeActivity.class));
}
});
modelMains = new ArrayList<>();
adapterMain = new AdapterMain(modelMains);
tableList.setAdapter(adapterMain);
adapterMain.addItem(new ModelMain("",0,true),modelMains.size());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
protected void onResume() {
super.onResume();
adapterMain.setOnItemClickListener(new AdapterMain.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
if (modelMains.get(position).isBtnPlus()){
adapterMain.addItem(new ModelMain("",0,true),modelMains.size());
}else {
adapterMain.deleteItem(position);
}
}
});
}
}
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_above="@+id/btnNext">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Estimated Job Time"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="5dp"
android:layout_margin="10dp">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:entries="@array/spinnerItems"/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/tableList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_marginTop="20dp"
android:padding="10dp"
android:gravity="right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTotalPrice"
android:text="0"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginLeft="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" AED"
android:textSize="25sp"
android:textStyle="bold"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Note"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Write Few Words About Services To Be Done By You"/>
</LinearLayout>
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/btnNext"
android:text="Submit To Customer"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"/>
</RelativeLayout>
<强> row_main_list.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="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/linPrimary"
android:weightSum="2"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:id="@+id/txtService"
android:textSize="18sp"
android:background="@drawable/rectangle_design"
android:textColor="@android:color/black"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="0"
android:gravity="center"
android:inputType="number"
android:padding="10dp"
android:id="@+id/txtPrice"
android:textSize="18sp"
android:background="@drawable/rectangle_design"
android:textColor="@android:color/black"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center"
android:id="@+id/linBtn">
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:id="@+id/btnAdd"
android:text="+"
android:textSize="18sp"
android:background="@drawable/button_design_plus"/>
</LinearLayout>
</LinearLayout>
<强> button_design_plus.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="@color/colorPrimary"/>
<corners android:radius="5dp" />
</shape>
</item>
</selector>
<强> rectangle_design.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@android:color/black" />
</shape>