我必须用两根手指滚动ScrollView

时间:2018-09-08 18:19:58

标签: android scrollview

我的滚动视图正常工作,但是经过一段时间的编码后,它不再起作用了,现在我不得不用两根手指滚动。

准确地说,我必须将手指放在屏幕上(不动),并用另一根手指滚动。

我不知道该怎么办。

这是XML文件:

<android.support.constraint.ConstraintLayout 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:layout_height="match_parent"
    android:background="#fff"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">


    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <LinearLayout
            android:id="@+id/linearParent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:scrollbars="none">

            <TextView
                android:id="@+id/Date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="01.01.2018"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:textStyle="bold"
                app:fontFamily="sans-serif" />

            <TextView
                android:id="@+id/Day"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Marti"
                android:textColor="@android:color/black"
                android:textSize="40sp"
                android:textStyle="bold"
                android:visibility="visible"
                tools:layout_editor_absoluteX="142dp"
                tools:layout_editor_absoluteY="193dp" />

            <LinearLayout
                android:id="@+id/InfoBox"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:orientation="vertical">

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

编辑:

这是Java文件

package com.maze_team.g3ru1a.poianamicului;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.maze_team.g3ru1a.poianamicului.alerts.AlertsActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Check Internet
        if(!isInternetAvailable()) Toast.makeText(getApplicationContext(), "No Internet Available", Toast.LENGTH_LONG).show();


        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TextView dateField = (TextView) findViewById(R.id.Date);
        TextView dayField = (TextView) findViewById(R.id.Day);
        DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        setDayAndDate();

        getInfo((String) (new android.text.format.DateFormat()).format("yyyy-MM-dd", Calendar.getInstance().getTime()));
    }

    public void setDayAndDate(){
        TextView dateField = (TextView) findViewById(R.id.Date);
        TextView dayField = (TextView) findViewById(R.id.Day);
        DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

        Calendar calendar = Calendar.getInstance();
        Date todayDate = calendar.getTime();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        android.text.format.DateFormat df = new android.text.format.DateFormat();
        String dayString = (String) df.format("yyyy-MM-dd", todayDate);

        dateField.setText(dateFormat.format(todayDate));

        switch (day){
            case Calendar.MONDAY:
                dayField.setText(getString(R.string.day_monday));
                break;
            case Calendar.TUESDAY:
                dayField.setText(getString(R.string.day_tuesday));
                break;
            case Calendar.WEDNESDAY:
                dayField.setText(getString(R.string.day_wednesday));
                break;
            case Calendar.THURSDAY:
                dayField.setText(getString(R.string.day_thursday));
                break;
            case Calendar.FRIDAY:
                dayField.setText(getString(R.string.day_friday));
                break;
            case Calendar.SATURDAY:
                dayField.setText(getString(R.string.day_saturday));
                break;
            case Calendar.SUNDAY:
                dayField.setText(getString(R.string.day_sunday));
                break;
        }
    }

    public String getDayOfWeekReadable(int day){
        switch (day){
            case Calendar.MONDAY:
                return getString(R.string.day_monday);
            case Calendar.TUESDAY:
                return getString(R.string.day_tuesday);
            case Calendar.WEDNESDAY:
                return getString(R.string.day_wednesday);
            case Calendar.THURSDAY:
                return getString(R.string.day_thursday);
            case Calendar.FRIDAY:
                return getString(R.string.day_friday);
            case Calendar.SATURDAY:
                return getString(R.string.day_saturday);
            case Calendar.SUNDAY:
                return getString(R.string.day_sunday);
        }
        return "";
    }

    public boolean isInternetAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        return cm.getActiveNetworkInfo() != null;
    }

    public void getInfo(String day){
        TextView dateField = (TextView) findViewById(R.id.Date);
        final TextView dayField = (TextView) findViewById(R.id.Day);
        final LinearLayout infoBox = (LinearLayout) findViewById(R.id.InfoBox);
        final LinearLayout parentBox = (LinearLayout) findViewById(R.id.linearParent);

        infoBox.removeAllViews();

        String url = "http://dev.maze-team.com/pmmap/calendar/getinfo/" + day;
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);

        JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        try{
                            infoBox.removeAllViews();
                            for(int i=0; i<response.length();i++){
                                TextView space = new TextView(getApplicationContext());
                                space.setText("");
                                space.setTextSize(20);

                                infoBox.addView(space);

                                JSONObject dateInfo = response.getJSONObject(i);

                                String colorString = dateInfo.getString("color");

                                TextView hours = new TextView(getApplicationContext());
                                hours.setGravity(Gravity.CENTER);
                                hours.setTextSize(26);
                                hours.setTextColor(Color.parseColor(colorString));
                                hours.setText(dateInfo.getString("hour"));

                                TextView title = new TextView(getApplicationContext());
                                title.setGravity(Gravity.CENTER);
                                title.setTextSize(30);
                                title.setTextColor(Color.parseColor(colorString));
                                title.setTypeface(null, Typeface.BOLD);
                                title.setText(dateInfo.getString("title"));

                                TextView details = new TextView(getApplicationContext());
                                details.setGravity(Gravity.CENTER);
                                details.setTextSize(22);
                                details.setTextColor(Color.parseColor(colorString));
                                details.setText(dateInfo.getString("details"));

                                TextView language = new TextView(getApplicationContext());
                                language.setGravity(Gravity.CENTER);
                                String lang = dateInfo.getString("lang");
                                if(lang.equals("[RO]")) language.setTextColor(Color.RED);
                                else language.setTextColor(Color.BLUE);
                                language.setText(lang);

                                LinearLayout info = new LinearLayout(getApplicationContext());
                                info.setOrientation(LinearLayout.VERTICAL);
                                info.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.bordered));
                                info.addView(hours);
                                info.addView(title);
                                info.addView(details);
                                info.addView(language);

                                infoBox.addView(info);
                                //Proccess Info
                            }

                        }catch(final JSONException e){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
//                                    Toast.makeText(getApplicationContext(),
//                                            "Json parsing error: " + e.getMessage(),
//                                            Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
//                        Toast.makeText(getApplicationContext(),
//                                "Errror"+error,
//                                Toast.LENGTH_LONG).show();

                    }
                });
        queue.add(jsonObjectRequest);
    }

    public void getWeekInfo(String day){
        TextView dateField = (TextView) findViewById(R.id.Date);
        final TextView dayField = (TextView) findViewById(R.id.Day);
        final LinearLayout infoBox = (LinearLayout) findViewById(R.id.InfoBox);

        String url = "http://dev.maze-team.com/pmmap/calendar/getweekinfo/" + day;
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);

        JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        try{
                            infoBox.removeAllViews();
                            for(int i=0; i<response.length();i++){
                                JSONArray respDay = response.getJSONArray(i);

                                TextView space = new TextView(getApplicationContext());
                                space.setText("");
                                space.setTextSize(10);

                                infoBox.addView(space);

                                TextView dayView = new TextView(getApplicationContext());
                                dayView.setGravity(Gravity.CENTER);
                                dayView.setTextSize(20);
                                if(i != response.length()-1) dayView.setText(getDayOfWeekReadable(i+2));
                                else dayView.setText(getDayOfWeekReadable(1));

                                LinearLayout info = new LinearLayout(getApplicationContext());
                                info.setPadding(10,10,10,10);
                                info.setOrientation(LinearLayout.VERTICAL);
                                info.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.bordered));
                                info.setMinimumHeight(400);
                                info.addView(dayView);

                                if(respDay.length() != 0){

                                    for(int j=0; j < respDay.length(); j++){
                                        JSONObject respInfo = respDay.getJSONObject(j);

                                        TextView inf = new TextView(getApplicationContext());
                                        inf.setGravity(Gravity.CENTER);
                                        inf.setTextColor(Color.parseColor(respInfo.getString("color")));
                                        inf.setText(respInfo.getString("hour")+" | "+respInfo.getString("title")+ " | " +respInfo.getString("lang"));
                                        info.addView(inf);
                                        //Design HERE

                                    }
                                }

                                infoBox.addView(info);
                            }

                            TextView space = new TextView(getApplicationContext());
                            space.setText("");
                            space.setTextSize(20);

                            infoBox.addView(space);
                        }catch(final JSONException e){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),
                                            "Json parsing error: " + e.getMessage(),
                                            Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(),
                                "Errror"+error,
                                Toast.LENGTH_LONG).show();

                    }
                });
        queue.add(jsonObjectRequest);
    }

    public void getMonthInfo(String day){
        TextView dateField = (TextView) findViewById(R.id.Date);
        final TextView dayField = (TextView) findViewById(R.id.Day);
        final LinearLayout infoBox = (LinearLayout) findViewById(R.id.InfoBox);

        infoBox.removeAllViews();

        CalendarView cv = new CalendarView(getApplicationContext());
        cv.setBackgroundColor(Color.WHITE);
        infoBox.addView(cv);

        TextView space = new TextView(getApplicationContext());
        space.setText("");
        space.setTextSize(20);

        infoBox.addView(space);

        final LinearLayout infoParent = new LinearLayout(getApplicationContext());
        infoParent.setOrientation(LinearLayout.VERTICAL);

        infoBox.addView(infoParent);

        cv.setOnDateChangeListener( new CalendarView.OnDateChangeListener() {
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

                //Toast.makeText(getApplicationContext(), year + "-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_LONG).show();

                String url = "http://dev.maze-team.com/pmmap/calendar/getinfo/" + year + "-"+(month+1)+"-"+dayOfMonth;
                // Instantiate the RequestQueue.
                RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

                JsonArrayRequest jsonObjectRequest = new JsonArrayRequest
                        (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {

                            @Override
                            public void onResponse(JSONArray response) {
                                try{
                                    infoParent.removeAllViews();
                                    for(int i=0; i<response.length();i++){

                                        JSONObject dateInfo = response.getJSONObject(i);

                                        String colorString = dateInfo.getString("color");

                                        TextView hours = new TextView(getApplicationContext());
                                        hours.setGravity(Gravity.CENTER);
                                        hours.setTextSize(18);
                                        hours.setTextColor(Color.parseColor(colorString));
                                        hours.setText(dateInfo.getString("hour"));

                                        TextView title = new TextView(getApplicationContext());
                                        title.setGravity(Gravity.CENTER);
                                        title.setTextSize(18);
                                        title.setTextColor(Color.parseColor(colorString));
                                        title.setTypeface(null, Typeface.BOLD);
                                        title.setText(dateInfo.getString("title"));

                                        TextView details = new TextView(getApplicationContext());
                                        details.setGravity(Gravity.CENTER);
                                        details.setTextSize(16);
                                        details.setTextColor(Color.parseColor(colorString));
                                        details.setText(dateInfo.getString("details"));

                                        TextView language = new TextView(getApplicationContext());
                                        language.setGravity(Gravity.CENTER);
                                        String lang = dateInfo.getString("lang");
                                        if(lang.equals("[RO]")) language.setTextColor(Color.RED);
                                        else language.setTextColor(Color.BLUE);
                                        language.setText(lang);

                                        LinearLayout info = new LinearLayout(getApplicationContext());
                                        info.setOrientation(LinearLayout.VERTICAL);
                                        info.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.bordered));
                                        info.addView(hours);
                                        info.addView(title);
                                        info.addView(details);
                                        info.addView(language);

                                        infoParent.addView(info);

                                        TextView space = new TextView(getApplicationContext());
                                        space.setText("");
                                        space.setTextSize(20);

                                        infoParent.addView(space);

                                        //Proccess Info
                                    }

                                }catch(final JSONException e){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            infoParent.removeAllViews();
//                                    Toast.makeText(getApplicationContext(),
//                                            "Json parsing error: " + e.getMessage(),
//                                            Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }

                            }
                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                infoParent.removeAllViews();
//                        Toast.makeText(getApplicationContext(),
//                                "Errror"+error,
//                                Toast.LENGTH_LONG).show();

                            }
                        });
                queue.add(jsonObjectRequest);

            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Calendar calendar = Calendar.getInstance();
        Date todayDate = calendar.getTime();
        android.text.format.DateFormat df = new android.text.format.DateFormat();
        String todayStr = (String) df.format("yyyy-MM-dd", todayDate);
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.todayButton:
                getInfo(todayStr);
                return true;
            case R.id.weekButton:
                getWeekInfo(todayStr);
                return true;
            case R.id.monthButton:
                getMonthInfo(todayStr);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_calendar) {
            // Go to big calendar
        } else if (id == R.id.nav_alerte) {
            Intent alertsIntent = new Intent(this, AlertsActivity.class);
            startActivity(alertsIntent);
        } else if (id == R.id.nav_contact) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://poianamicului.ro/kontaktu.htm"));
            startActivity(browserIntent);
        } else if (id == R.id.nav_noutati) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://poianamicului.ro/"));
            startActivity(browserIntent);
        } else if (id == R.id.nav_livefeed) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://media.ercis.ro/poianamicului.html"));
            startActivity(browserIntent);
        } else if (id == R.id.nav_cazare) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://poianamicului.ro/casapelerinului/casapelerinului.htm"));
            startActivity(browserIntent);
        } else if (id == R.id.nav_cantece) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://cant.bukur.ro/"));
            startActivity(browserIntent);
        } else if (id == R.id.nav_facebook) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/parohia.poianamicului/"));
            startActivity(browserIntent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

编辑2:

它甚至更奇怪,我改为放置了一个webview,并且我必须做同样的事情才能在上面滚动。

该死的。

2 个答案:

答案 0 :(得分:0)

听起来像在设备上启用了对讲功能。

答案 1 :(得分:0)

尝试注释Java代码,然后在设备上运行该应用程序。通过这种方式,我的意思是说您的xml代码很好,但是您的java代码却在造成问题。因此,创建一个新项目并在其中粘贴相同的xml代码,并检查滚动是否可行。